Open sabi0 opened 6 years ago
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.
Or is it some special (Kotlin?) syntax when "Elvis" condition can be a part of a bigger expression?