-
Notifications
You must be signed in to change notification settings - Fork 34
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
return more than one value from a for-comprehension #860
Comments
It's called
Interesting. So basically you're providing |
I love how you can say |
Be careful with what you say, he might just rename |
Oh I realize that, but then I would have to find a new name for the arguably deeper and more basic operation that is currently called |
OK, so this issue us actually way more important than I had realized. For #807, we need something almost exactly like this. Unfortunately, the syntax that makes sense for #807 is this: Div {
for (sect in sections) {
Title(sect.title),
Text(sect.text)
}
} Not, as proposed by @FroMage: Div {
for (sect in sections) *{
Title(sect.title),
Text(sect.text)
}
} But the first syntax has a huge problem. It naturally means a comprehension producing iterables! Now I'm regretting not having required an extra punctuation character in our comprehension syntax, for example: [ for (i in 1..10) : i^2 ] That would have spared us from this ambiguity :-( |
FTR, today you can write this as: Div {
*expand {
for (sect in sections) {
Title(sect.title),
Text(sect.text)
}
}
} But this involves a whole extra level of nesting, and two rather technical constructs, the |
I think this will become part of a bigger debate over the syntax, because I presume people will also want to be able to do: Div {
for (sect in sections) {
Title(sect.title),
Text(sect.text)
},
for (para in paragraphs) {
Title(para.title),
Text(para.text)
},
if (needsSomething) {
Text(something)
}
} |
That's #807 of which this is just a part. |
So there is a workaround, with what we have today. A framework could define a Div {
for (sect in sections)
Fragment {
Title(sect.title),
Text(sect.text)
}
} That's not perfect, but I think it's OK. |
Well, hrm, @drochetti is already way ahead of me here and he actually already supports almost the same idea in Div {
for (sect in sections) {
Title(sect.title),
Text(sect.text)
}
} The downside of that is that |
A further alternative would be to put the Div {
*for (sect in sections) {
Title(sect.title),
Text(sect.text)
}
} With: Div {
for (sect in sections) *{
Title(sect.title),
Text(sect.text)
}
} |
I know we can do that with something that flattens the resulting comprehension afterwards, but it could be useful to support "returning" more than one value from a comprehension:
Where I reuse the
*
operator to spread more than one value in the comprehension.The text was updated successfully, but these errors were encountered: