-
Notifications
You must be signed in to change notification settings - Fork 62
iteration and branching in named argument lists #3913
Comments
[@gavinking] This issue is very related, but nevertheless orthogonal, to #3855. |
[@gavinking] A solution to this issue would be to:
Then we would wind up with something like this: Div {
Div { Span("Items") },
Div {
Hr(),
*if (items.empty) then {
Span(""),
Span("no items"),
Hr()
}
else {
for (i->item in items.indexed) *{
Span("``i``."),
Span(item.description),
Hr()
}
}
}
} The problem with this are the nasty P.S. A related idea is to allow comprehensions that begin with an
Unlike |
[@gavinking] So, over on #3966, I concluded that maybe a Div {
Div { Span("Items") },
Div {
Hr(),
if (items.empty)
Fragment {
Span(""),
Span("no items"),
Hr()
}
else
Fragment {
for (i->item in items.indexed)
Fragment {
Span("``i``."),
Span(item.description),
Hr()
}
}
}
}
} This is probably better than the |
[@gavinking] Y'know, if we consider the Div {
Div { Span("Items") },
Div {
Hr(),
if (items.empty) *{
Span(""),
Span("no items"),
Hr()
}
else *{
for (i->item in items.indexed) *{
Span("``i``."),
Span(item.description),
Hr()
}
}
}
} |
[@gavinking] Well, accommodating Div {
Div { Span("Items") },
Div {
Hr(),
if (items.empty) *{
Span(""),
Span("no items"),
Hr()
},
if (!items.empty)
for (i->item in items.indexed) *{
Span("``i``."),
Span(item.description),
Hr()
}
}
} That's honestly not bad. And all it requires is:
That's really a pretty minor list of changes... |
[@gavinking] Perhaps I prefer this variation: Div {
Div { Span("Items") },
Div {
Hr(),
*if (items.empty) {
Span(""),
Span("no items"),
Hr()
},
*if (!items.empty)
for (i->item in items.indexed) {
Span("``i``."),
Span(item.description),
Hr()
}
}
} |
[@luolong] To be honest, syntax wise I would actually prefer this variant: Div {
Div { Span("Items") },
Div {
Hr(),
*{ if (items.empty) {
Span(""),
Span("no items"),
Hr()
}},
*{ if (!items.empty)
for (i->item in items.indexed) {
Span("``i``."),
Span(item.description),
Hr()
}
}
}
} |
[@gavinking] Actually there is a Good Reason to prefer the upfront
With:
Since |
[@gavinking] @luolong |
[@gavinking] I'm going to close this issue, since the solution is now captured by #3966, #3975, and #3974. |
[@gavinking] To my surprise there is no actual issue dealing with this topic, though it has been discussed episodically over the last 4 years, and even used to be the subject of a TODO in the spec, before it got "cleaned up" one day.
We already have limited support for doing branching and looping inside a named argument list, using:
then
/else
.With enhancements already planned for 1.2, we will also get real conditional expressions.
For example:
However, that's still not going to be quite enough to let us write really freeform stuff in the section of the argument list that accepts a list of arguments. When we get a chance to spend some time on this, we need to figure out exactly how to support more than just a single comprehension there, allowing a mix of:
For example:
The above is not possible today because you can't have multiple children hanging off a conditional expression or comprehension and because comprehensions cannot be mixed with other arguments.
It's easy to just say that the solution to this problem is to allow freeform
for
loops andif
/switch
conditionals in a named argument list, and interpret its "statements" as argument expressions. But saying it like that hides a whole lot of things that need to be thought through properly.In particular, we need to understand how this syntax relates to comprehensions as they exist today. It seems to me that the basic difference is that a comprehension or
if
expression expects a single expression at the end of it whereas for templating it's often more convenient to have more than one thing produced by a branch of the conditional or iteration of the loop. Hence the difference between the two code examples above is the braces.I'm also a bit confused as to what is the natural punctuation for this. Commas, or semis? It's not clear which would be more consistent. Commas, probably.
[Migrated from ceylon/ceylon-spec#807]
[Closed at 2013-12-04 14:12:13]
The text was updated successfully, but these errors were encountered: