-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Shorthand for fns that immediately match #1577
Comments
It does seem like a bit of a special case though... What if match was defined to be an inlined function call... That is |
@mark-i-m You mean |
Yes, or rather an implicit closure On Aug 29, 2016 9:38 AM, "Steven Allen" [email protected] wrote:
|
Now, that I think about... I think we would have to clarify when On Aug 29, 2016 9:51 AM, "Mark Ishak Mansi" [email protected] wrote:
|
just writing |
This thing would be simply wonderful! I'm writing Scala on my main job, and every time I return to writing Rust I miss the shorthand syntax for closures very badly. |
I am wondering if there would be any need/way to also have a shorthand for |
@mark-i-m yes, a good point. of course we could permit one to just use |
Hmmm... yes, but it does seem to mar the usual elegance of rust syntax... I am beginning to become a bit opposed to the idea of adding special syntax... |
At present a code block can appear as a match expression, like if you write a Rust has issues with currying, right? Just with kinds or even more basic? I've encountered reborrowing problems with doing say
I donno if they could be fixed by trying harder though, but I doubt tuples work either. As a result, we might be stuck as one parameter for this. If so, then closure notations If you do need notation for non-closures, then maybe
but obviously this would benefit form the Haskell, etc. style detached function types. |
Frankly, I think if this sort of shorthand is to be done, it should be possible to promote any expression into a closure, not just match blocks. And to my knowledge this is possible with Scala's _ notation, right? |
+1 to borrowing from scala on this one |
It'd be more useful if it could be used for both closures and functions and for all possible arities, e.g.: fn foo(match) {
(Some(n)) => n,
(None) => 0,
}
let bar = |match| {
(Some(n), _) => n,
(None, k) => k,
};
println!("{}, bar(None, 42)); That way, Rust would get not just a foo (Just n) = n
foo Nothing = 0 |
On the other hand it's also a good idea to avoid introducing too many special cases for a minor gain in succinctness... especially if this introduces pitfalls in the language. |
how about |
That doesn't really help for closures, though, right?
…On Aug 11, 2017 4:08 PM, "dobkeratops" ***@***.***> wrote:
how about fn foo(...) = match { pat1=>expr1, pat2=>expr2, ... } ... sugar
for single expression functions , dropping a nesting level. The other place
this might be nice is constructors, e.g. fn make_bar(..) = Bar{ ... }
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1577 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AIazwLqRatfV1luNX0eRiypQmd88U1U6ks5sXLRCgaJpZM4IDY2s>
.
|
Had a discussion with @Centril about this idea yesterday; I independently came up with @dobkeratops's idea. My personal leaning here is two features:
|
Would
|
I'm not strongly opposed to inferring the return type, but yes, I left it out. I think that, apart from sharing a goal of brevity, inferred return types are completely orthogonal as a feature. |
Just discovered this gh issue. Big fan of this in what ever form it may take. I transitioned into rust from scala and miss this very much.
has always felt more redundant than what the analog might look like from scala ( just using the body of the match block )
|
Although I didn't initially agree with the original post, after writing Rust for a while I can see why this would be convenient for some. There are some parts that get pretty redundant (especially when type names are involved), and the rightward drifting of code becomes too common. In terms of syntactic style, something like this feels like it'd fit right in: fn unwrap(opt: Option<i32>) -> match opt {
Some(n) => n,
None => panic!(),
} No redundant braces, less indenting, and it's easy to read (for me at least). Similarly, I've written code that looks like this: fn new() -> Struct {
Struct {
field: 1,
// more fields...
}
} which is a very common pattern, but for something that simple I wish I could write this instead: fn new() -> Struct {
field: 1,
// more fields...
} That said, something like this would require some combination of an optional/inferred return type for functions, and allowing any expression after an An alternative, as @alercah mentioned, would be to use Possible odd cases: struct S; // unit struct
fn new() -> S; // valid, but is useless and looks like a constructor with no code // ambiguous integer type unless suffixed or left for compiler to choose at the end.
// ...feature?
fn nice() -> 69; |
May be worth noting that OCaml and F# also have this shorthand: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/match-expressions. |
Has there been any updates on this? |
Inferred return types in function signatures is a foot gun. Remember Steve Klabnick's Rust's Golden Rule. |
I think for private functions inferring return types is probably fine. For public functions I agree that having explicit signatures is important for documenting the API contract. |
There is a desire to have some sort of shorthand for functions that immediately match on their argument. Currently, one must write:
or
It would be nice to be able to elide the
match
(and perhaps not even give a name to the parameterx
). Many functional languages like Haskell and Scala have these sorts of shorthands.Related RFCs:
The text was updated successfully, but these errors were encountered: