Noncommutative null in "then - else"

40 views
Skip to first unread message

Nikolay Rychkov

unread,
Jul 6, 2015, 12:34:17 PM7/6/15
to ceylon...@googlegroups.com
Why 
Integer?[] test = [for (i in -3 .. 3) i > 0 then i else null];
is ok, otherwise
Integer?[] test = [for (i in -3 .. 3) i > 0 then null else i];
causes errors  "Operand expression may not be an optional type: null is not assignable to Object" and "Expression must be of optional type: Null is not optional" 

John Vasileff

unread,
Jul 6, 2015, 12:44:04 PM7/6/15
to ceylon...@googlegroups.com
I believe, if allowed, ` i > 0 then null else i` would always evaluate to `i`, which would be confusing.


--
You received this message because you are subscribed to the Google Groups "ceylon-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users...@googlegroups.com.
To post to this group, send email to ceylon...@googlegroups.com.
Visit this group at http://groups.google.com/group/ceylon-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceylon-users/876c5278-31cb-4fe6-8ae3-7667db6ca37b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gavin King

unread,
Jul 6, 2015, 2:05:55 PM7/6/15
to ceylon...@googlegroups.com

Gavin King

unread,
Jul 6, 2015, 2:06:43 PM7/6/15
to ceylon...@googlegroups.com
FTR, use this instead:

Integer?[] test = [for (i in -3 .. 3) if (i > 0) then i else null];

Nikolay Rychkov

unread,
Jul 6, 2015, 2:11:42 PM7/6/15
to ceylon...@googlegroups.com
To tell the truth it is not clear why ` i > 0 then null else i` would always evaluate to `i`

понедельник, 6 июля 2015 г., 19:44:04 UTC+3 пользователь John Vasileff написал:
I believe, if allowed, ` i > 0 then null else i` would always evaluate to `i`, which would be confusing.

On Jul 6, 2015, at 12:34 PM, Nikolay Rychkov <nikolay...@gmail.com> wrote:

Why 
Integer?[] test = [for (i in -3 .. 3) i > 0 then i else null];
is ok, otherwise
Integer?[] test = [for (i in -3 .. 3) i > 0 then null else i];
causes errors  "Operand expression may not be an optional type: null is not assignable to Object" and "Expression must be of optional type: Null is not optional" 


--
You received this message because you are subscribed to the Google Groups "ceylon-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users...@googlegroups.com.
To post to this group, send email to ceylo...@googlegroups.com.

Lucas Werkmeister

unread,
Jul 6, 2015, 2:16:33 PM7/6/15
to ceylon...@googlegroups.com
`then` and `else` are separate constructs. `i > 0 then null else i` means `((i > 0) then null) else i`. First ,`(i > 0)` yields `true`. Then, `true then null` would yield null. And finally, `null else i` would yield `i`, since that’s what `else` does – it yields the right-hand side iff the left-hand side is `null`.

`i > 0 then i` should work though – no need for `else null`.
To post to this group, send email to ceylon...@googlegroups.com.

Gavin King

unread,
Jul 6, 2015, 2:22:26 PM7/6/15
to ceylon...@googlegroups.com
Because " i > 0 then null else i" means "(i > 0 then null) else i"
according to the precedence rules of the then and else operators. And
the else operator returns its second argument whenever its first
argument evaluates to null.

On Mon, Jul 6, 2015 at 8:11 PM, Nikolay Rychkov
> To post to this group, send email to ceylon...@googlegroups.com.
> Visit this group at http://groups.google.com/group/ceylon-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ceylon-users/5be763ae-3c25-4094-8799-f3e86edafa2a%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



Nikolay Rychkov

unread,
Jul 6, 2015, 2:30:32 PM7/6/15
to ceylon...@googlegroups.com
Thank you. Now is clear.

понедельник, 6 июля 2015 г., 21:16:33 UTC+3 пользователь Lucas Werkmeister написал:
Reply all
Reply to author
Forward
0 new messages