-
Notifications
You must be signed in to change notification settings - Fork 13k
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
RFC: Require explicit syntax to declare boxed, unique closures #1864
Comments
How about arrow-based syntax? That could look nice enough to also replace
|
I concur that the first point is a clear win; it's important to acknowledge that there are times when people simply don't care to specify the argument and return types of their closures. +1 to allowing parens to be omitted when there are no arguments. This is really no different from omitting the I think that paren-less calls for trailing closures are one of Rust's "hey, that's pretty neat" features, but I'm not especially attached to them (though I'd be a little sad to see them go). However this one is decided, the stack closure syntax should work the same way. |
I really like that the arrow syntax has a natural place to stuff the kind sigil, but combined with the sugar for trailing lambdas it is pretty confusing.
|
I actually kinda like the block form we have now, since it makes both the parameter names and the curly-open wind up on the line with the caller. Makes "pass a closure" read more like built-in control forms. That's its charm, anyway. But the argument about explicit-ness is fair; just off the cuff, how about supporting sigils in there?
I also agree some with @Wensleydale that I'm ... often ok letting it be inferred (because I'm not planning on doing any environment capture, or nothing that would notice any difference) |
@brson and I were tossing around a variant on @marijnh's arrow syntax. Something like this:
In use it would look like:
With no arguments, the arrow is still required:
There are some problems:
|
@graydon also explicit sigils might be ok. Mostly I was afraid of this:
where the |
Surely The arbitrary lookahead scanning the argument list seems like a pretty big negative concerning arrows, to me. I realize this is "arguing over syntax", and am certainly willing to talk through further options; but I'd prefer the parser gets simpler over time (as we figure out nice ways to regularize and formalize what it's doing) rather than more complex. |
@graydon - spawn always infers If we are going to keep the inference then adding the sigils is less important, though it probably will allow block syntax in places where we currently have to use the long syntax. It's a shame that this will add yet more ways to write functions. If we want to keep the kind inference then I think my inclination is to leave the block syntax alone and make the long syntaxes easier to use by allowing both the argument types and return types to be omitted. |
Some thoughts on the matter:
|
Note that capture clauses will be needed more often if we stop implicitly copying non-trivial data structures (though the |
I'm closing this, as there has been no motion forward. |
So, currently, the sugared closure syntax
{||...}
can be used for any kind of closure. @brson and I wanted this so that task spawning looks nice:but I've since soured on it (and I think he has too). The basic reason is that it is not explicit whether the closure is a boxed or unique closure: this depends on the definition of the callee function. But this distinction is important to the semantics and cost model. Therefore, I would like to make it so that
{||...}
is always a stack closure (fn&
).With no other changes, this would mean that spawning a task is written
task::spawn(fn~() {...})
. To makefn@()
andfn~()
a little more convenient to use, however, we could make any or all of the following changes:task::spawn(fn~ {...})
){||...}
(e.g.,task::spawn fn~ {...}
ortask::spawn fn~() {...}
)This RFC is truly a "request for comment" because I'm not really sure what's best. Should we just leave things as they are? If not, do we implement the proposals above to lighten the syntax a bit? I think the first point (omitting variable types) is a clear win, but I'm less sure about #2 and #3. The fully explicit form is perhaps not so bad.
p.s. Another option might be to add some sigil to the sugared form, but
task::spawn {~|| ...}
just looks like a lot of random characters in a row to me.The text was updated successfully, but these errors were encountered: