Michael Giagnocavo
2014-10-13 05:41:09 UTC
I came across the "if let" syntax, and it's been bothering me a bit.
It seems like a strange thing to elevate to a grammar-level construct.
The example given is:
if let Some(x) = foo() {
doSomethingWith(x)
}
What's wrong with e.g.:
foo().if_some!(|x| { doSomethingWith(x) })?
Can macros not step in here and allow us to write code that looks like a
closure but doesn't actually create a closure or cost any overhead?
(In fact, is a macro even needed? Couldn't a function and it's function
parameters be marked for inlining - wouldn't that take care of it?) This
seems like a handy thing to have in general. I've written code in other
languages where I want the emitted code to be as if I wrote it as a
single function with branches, but want to express it with lambdas
and function calls.
If neither inlining nor macros can handle this, would it not be better
to improve those features, instead of tacking on odd bits of grammar?
I love more features and expressiveness but "if let" seems like
something that shouldn't be baked into the compiler (even if Swift does it).
I'm honestly asking this question, because the Rust team seems to have
pretty good sense and reasoning, so I'm probably misunderstanding something
and would like to know what I'm missing.
Sincerely,
Michael
It seems like a strange thing to elevate to a grammar-level construct.
The example given is:
if let Some(x) = foo() {
doSomethingWith(x)
}
What's wrong with e.g.:
foo().if_some!(|x| { doSomethingWith(x) })?
Can macros not step in here and allow us to write code that looks like a
closure but doesn't actually create a closure or cost any overhead?
(In fact, is a macro even needed? Couldn't a function and it's function
parameters be marked for inlining - wouldn't that take care of it?) This
seems like a handy thing to have in general. I've written code in other
languages where I want the emitted code to be as if I wrote it as a
single function with branches, but want to express it with lambdas
and function calls.
If neither inlining nor macros can handle this, would it not be better
to improve those features, instead of tacking on odd bits of grammar?
I love more features and expressiveness but "if let" seems like
something that shouldn't be baked into the compiler (even if Swift does it).
I'm honestly asking this question, because the Rust team seems to have
pretty good sense and reasoning, so I'm probably misunderstanding something
and would like to know what I'm missing.
Sincerely,
Michael