-
Notifications
You must be signed in to change notification settings - Fork 62
if expressions #3609
Comments
[@gavinking] When we do this and #3662, I think we should consider also supporting
|
[@tombentley] My only concern is that the similarity between
and
will result in bugs (when the code is written) and misinterpretation (when it's read). |
[@gavinking] @tombentley Wellyes, but fortunately that's the kind of bug the typechecker can catch. |
[@ncorai] Would if expressions accept condition lists, e.g. |
[@gavinking] @ncorai I assume so. That would be regular. |
[@gavinking] Now implemented in the |
[@sgalles] Now, I was wondering if it could be useful to have an inline throw Nothing error(String msg) {
throw AssertionError(msg);
}
{Integer*} numbers = "1 2 3".split().map((e) => parseInteger(e) else error("'``e``' can not be parsed")); It's nice, but sometimes maybe I'd like to to write : {Integer*} numbers2 = "1 2 3".split().map((e) => parseInteger(e) else throw AssertionError("``e`` should be a number")); |
[@quintesse] @sgalles definitely, but people seemed very much against it in this old discussion: #3757 |
[@sgalles] Oh, thanks @quintesse I had missed this discussion |
[@FroMage] So what's the syntax? |
[@gavinking] if (whatever) then foo else bar |
[@FroMage] Are any branches optional? |
No. |
That's not what you put in the issue description, why did you change your mind? |
[@luolong] What would be the return type of the if extpression without else? |
[@gavinking] Well the type of |
[@ncorai] I've always thought the distinction between |
[@jvasileff] What is the status if then/else in light of this? The reason I ask is that one of the first things I did with Ceylon was to naively write a contains method that can reduce to: function contains(T item) => // Evaluates to true!
true then false // if lt, search left subtree
else false then false // else if gt, search right subtree
else true; // else must be == this node which of course is missing a set of parenthesis and produces incorrect results. I don't have a strong opinion on this, and I do like the brevity of then/else. But if if/then/else winds up being seen as a safer or easier to read replacement, then I think else should be optional. |
[@chochos] Are these new condition expressions going to do type narrowing as well? |
[@chochos] Done for JS. |
[@chochos] Um... the |
[@gavinking] Right. It's not optional. |
[@gavinking] Done. |
[@gavinking] By considering the grammar of the language, and the precedences of its symbols, I arrived at the following syntax as the only really reasonable way to accommodate
exists
,nonempty
, andis Type
within the expression syntax.All of the above would be expressions, at the same precedence level as the existing
then
/else
operators.Notes:
if
condition can be any one of the usual kinds of conditions in Ceylon:exists
,nonempty
,is Type
, or boolean.else
clause is optional. If it is missing, the whole expression evaluates tonull
.then
clause is required.The
then
keyword is necessary to help disambiguate the comprehensions syntax. The following expressions are all different:The first expression filters out input elements which are
null
. The second expression results in anull
resulting element for eachnull
input element. The third expression results in the value1
for eachnull
input element.Additional advantages of
then
are:then
andelse
are disjunction expression, not full expressions.then
/else
operators.The parens are needed because of the existing relative precedence of
=
vs.then
/else
.This proposal would give @FroMage a piece of his #3563. To be clear, this would be a feature for Ceylon 1.1.
[Migrated from ceylon/ceylon-spec#503]
[Closed at 2014-11-16 18:07:34]
The text was updated successfully, but these errors were encountered: