Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upShorthand for fns that immediately match #1577
Comments
nrc
added
the
T-lang
label
Aug 18, 2016
This comment has been minimized.
This comment has been minimized.
|
It does seem like a bit of a special case though... What if match was defined to be an inlined function call... That is |
This comment has been minimized.
This comment has been minimized.
|
@mark-i-m You mean |
This comment has been minimized.
This comment has been minimized.
|
Yes, or rather an implicit closure On Aug 29, 2016 9:38 AM, "Steven Allen" notifications@github.com wrote:
|
This comment has been minimized.
This comment has been minimized.
|
Now, that I think about... I think we would have to clarify when On Aug 29, 2016 9:51 AM, "Mark Ishak Mansi" mmansi@utexas.edu wrote:
|
This comment has been minimized.
This comment has been minimized.
|
just writing |
This comment has been minimized.
This comment has been minimized.
netvl
commented
Aug 31, 2016
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. |
This comment has been minimized.
This comment has been minimized.
|
I am wondering if there would be any need/way to also have a shorthand for |
This comment has been minimized.
This comment has been minimized.
|
@mark-i-m yes, a good point. of course we could permit one to just use |
This comment has been minimized.
This comment has been minimized.
|
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... |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
burdges
commented
Jan 8, 2017
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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? |
This comment has been minimized.
This comment has been minimized.
tupshin
commented
Jan 22, 2017
|
+1 to borrowing from scala on this one |
This comment has been minimized.
This comment has been minimized.
Rufflewind
commented
Feb 13, 2017
|
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 |
This comment has been minimized.
This comment has been minimized.
leonardo-m
commented
May 30, 2017
|
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. |
This comment has been minimized.
This comment has been minimized.
dobkeratops
commented
Aug 11, 2017
•
|
how about |
This comment has been minimized.
This comment has been minimized.
|
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>
.
|
This comment has been minimized.
This comment has been minimized.
|
Had a discussion with @Centril about this idea yesterday; I independently came up with @dobkeratops's idea. My personal leaning here is two features:
|
This comment has been minimized.
This comment has been minimized.
burdges
commented
Aug 26, 2018
|
Would
|
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
softprops
commented
Nov 19, 2018
|
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 )
|
This comment has been minimized.
This comment has been minimized.
1011X
commented
Jan 2, 2019
|
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; |
This comment has been minimized.
This comment has been minimized.
samuela
commented
Mar 16, 2019
|
May be worth noting that OCaml and F# also have this shorthand: https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/match-expressions. |
nikomatsakis commentedApr 8, 2016
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: