Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong folding of the ternary operator #110

Open
sabi0 opened this issue Jan 31, 2018 · 1 comment
Open

Wrong folding of the ternary operator #110

sabi0 opened this issue Jan 31, 2018 · 1 comment

Comments

@sabi0
Copy link

sabi0 commented Jan 31, 2018

image

Or is it some special (Kotlin?) syntax when "Elvis" condition can be a part of a bigger expression?

@stoerr
Copy link

stoerr commented Aug 15, 2018

I too was puzzled by a strange folding related to the elvis operator. In Java and JSPs I stumbled over foldings of

Something something = res != null ? res.methodcall(whatever) : null;

to

val something = res?.methodcall(whatever) ?: null;

This is not quite correct. According to the Java elvis operator proposal A ?: B would unfold to A != null ? A : B , which does not fit that folding.

And the ?. operator, e.g. the save call operator in Kotlin, has A ?. B unfold to A != null ? A.B : null . This also doesn't fit the folding above.

I think the line should fold to

val something = res?.methodcall(whatever);

If you literally would unfold what it folds to now, you'd get

Something something = (res != null ? res.methodcall(whatever) : null) != null ? (res != null ? res.methodcall(whatever) : null) : null;

which makes no sense at all. 8-)

On second thought, this might be a different issue, but the original point also treats ?: wrongly. Actually, A ?: null is the same as A, so it might be sufficient to omit something like ?: null and use ?: only if there is something non-null after it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants