It's not only the efficiency boost. What about safe concurrency? Or about the tooling - does sbt really compare favorably to cargo? (I'd say "no", having used both over multiple years)
Suppose we require a keyword -- say var -- before all binding patterns. This results in having to write things like for (&(var x1, var y1, var z1), &(var x2, var y2, var z2)) in points.iter().tuple_windows() {},
which is quite a bit more verbose than the current for (&(x1, y1, z1), &(x2, y2, z2)) in points.iter().tuple_windows() {}.
Not to mention you'll have to write let var x = 0; just to declare a variable, unless you redesign the language to allow you to just write var x = 0 (and if you do that, you'll also have to somehow support a coherent way to express if let Some(x) = arr.pop() {} and let Some(x) = arr.pop() else {todo!()}).
Suppose we require a keyword -- say const -- before all value-matching patterns that look like variables. Then, what's currently
and you always run the risk of forgetting a const and accidentally binding a new match-all variable named None -- the main footgun that syntactically distinguishing binding and value-matching patterns was meant to avoid in the first place.
Suppose we require a sigil such as $ before one type of pattern. Probably the best solution in my opinion, but that's one symbol that can no longer be used for other things in a pattern context. Also, if you're already using sigils before variable names for other purposes (I've been sketching out a language where a pointer variable $x can be auto-dereferenced by writing x), doubling up is really unpleasant.
...So I can understand why Rust chose to give the same, most concise possible syntax for both binding and value-matching patterns. At least compiler warnings (unused, non-snake-case variables) are there to provide some protection from accidentally turning one into the other.
I went the "only let introduces bindings" route, and I'm pretty happy so far:
if (left.next(), right.next())
... is (Some(let l), Some(let r)) { /* use l and r */ }
... is (Some(let l), None ) { /* use l */ }
... is (None, Some(let r)) { /* use r */ }
... is (None, None ) { /* use nothing */ }
}
Or you could put the let before the Some(...) as let Some(l), which allows us to keep the current if let Some(...) = ... syntax. Either of those would feel more consistent than the current implementation.
I'm curious, have you used Rust much? Most of those changes just feel like "rust should be more familiar to me" changes.
Also:
As Rust 2.0 is not going to happen, Rust users will never get these language design fixes
Isn't necessarily true for most of your suggestions. Since most of them are just changes to syntax semantics and not language semantics they could be made in an edition.
I would like to take this a step further: support default arguments (fn func(a: int = 0); func(); func(a=0)), and then have struct initialization work like that. It's really nice in Python, though I'd like some rules to require args w/ default values to use the assignment syntax, and _require args w/o default values to be in positional order (our codebase is littered with long_func_name(long_var_name=long_var_name, ...) where function calls are incredibly long and redundant). That does a few things for us:
make defaults more obvious - no more T::new() since now you can just do T() if you just want defaults
make special cases obvious - func(1, 2, some_arg=value) makes it obvious that value is special
eliminates the T{...} syntax, since you can just use the T(...) syntax
We probably disagree about requiring positional args to use the assignment syntax, but both your preference and mine can be enforced w/ conventions (i.e. give your variables relevant names to the function).
Replace impl and eliminate the impl X for Y syntax
I disagree, though I can see where you're coming from. Many OOP languages use the first option, and the second try to reuse keywords.
But you're also missing one use of the impl keyword: fn func() -> impl Trait.
I come from Go and I honestly like the syntax there:
func (T self) fn_name(args...) {
}
Many don't. I like it because it shows that these methods aren't part of the type T, they're "attached" to it. That's much closer to how this actually works that how Java represents it. It's a small thing, but for something low-level like Rust, I think that makes sense.
Also, it makes room for implementing additional functionality on existing types outside of that package. This doesn't work exactly as I'd prefer, but the syntax opens the door to that.
Stop using macros to emulate varargs
I'm on the fence about this. The println!("{}", val) syntax is really nice, and that's one of the common uses for varargs. If your varargs are all the same type, you can use slices: fn func(varargs: &[T]); func(&[1, 2, 3]) and avoid macros entirely. If your types are inconsistent (i.e. C's varargs), you're going to have a rough time w/ any vararg syntax and will need a macro anyway.
I agree that macros shouldn't be abused to emulate varargs, but I don't think we actually need a vararg syntax and can use existing tools.
Drop range syntax
Agree. Use a built-in like Python's range(...) instead of fancy syntax, and have the type ([T], &[T], or vec<T>) be inferred at compile time. This can probably just drop-in to everywhere the range syntax is being used currently. It's a little more verbose, but it's way clearer.
Drop array and slice syntax
Are you suggesting using keywords/methods instead? So [T].slice(...) or even Array<T>.slice(...)? I think that's reasonable, and it can work similarly to Vec, but it would complicate the syntax a bit for my vararg proposal. But yeah, [T, count] is pretty ugly, but I'm not convinced Array<T, count> is all that much better.
I think your post could benefit from a couple examples.
Make generics use [] instead of <>/::<>
I prefer D's ! approach: T!U or T!(U). I would switch macros to T#(...) instead, since # is already used for things adjacent to macros anyway. But to minimize impact, we could just use # for generics instead.
I'm not a fan of the [] for generics, and I disagree w/ Go using that. <> is awkward due to conflicts with comparison operators, so I agree that it needs to go.
Fold Index and IndexMut into Fn trait family
Eh, I'm less interested in this one, but I don't have a strong opinion. Calling *T.index(...) isn't a big ask...
Remove the hierarchy between Eq/Ord and PartialEq/PartialOrd traits
This is certainly annoying, but surely the right solution is to just make floats implement Eq/Ord, no? I can understand someone preferring the PartialEq/PartialOrd behavior, but that's way more niche than just treating 0 and -0 as equal and NaN as always unequal.
Drop ::
Agreed.
Drop as
Disagree. I prefer changing it to only do type conversions (i.e. f32 -> f64 is valid, but f64 -> f32 isn't). Value conversions should use a different syntax.
Drop if-let
Disagree. Either keep as-is, or put let next to the new symbol (if Some(let i) = ...). I don't like chaining, if you want that, just introduce a block or something. It's a bit more verbose, but it's way less confusing.
Other languages have destructuring, and this is basically the same thing, and should be handled similarly. For example, in javascript: let {some: myVar} = option_object; (i.e. object that looks like {some: value}. An option is basically just an object w/ two members, so the syntax should be similar: let Some(my_var) = option_object else default_value;. Throwing an if in front should merely do something like Python's walrus operator, if my_var := some_call(), which evaluates whether the expression is truthy, but in the Rust case, it would evaluate whether the assignment was able to be made.
I think if-let makes sense, but don't expand it.
Drop procedure syntax
Why? What value does -> () provide? Why not elide that?
Rectify “almost rules” into “always rules”
This sounds pretty pedantic. Most of those are quite intuitive, and a lot of this is redundant given the rest of your post.
Remove significance of semicola
Disagree. I like that semicolons have meaning in Rust, because in most languages they just feel unnecessary, and languages w/o them feel awkward since you're generally limited to one statement per line.
requiring positional args to use the assignment syntax
Not sure, maybe my wording isn't clear enough.
What I intended to say is that arguments can be named, not that they have to.
In any case, the order of arguments must match the order of parameters, named or not.
But you’re also missing one use of the impl keyword: fn func() -> impl Trait.
That removal could actually happen, so I didn't list it. (Rust started requiring dyn and disallowed naked trait returns with edition 2018. So dropping the impl in that position might not be completely impossible like the other uses of impl.)
Are you suggesting using keywords/methods [for array and slice syntax] instead?
Yes, just methods.
I can understand someone preferring the PartialEq/PartialOrd behavior
You can have both – that's what's being made possible by them not being in a hierarchy.
I think if-let makes sense, but don’t expand it.
It's a bit late for that, isn't it? ;-)
Why? What value does -> () provide? Why not elide that?
What value is provided by keeping it? Why a syntactic special-case for exactly that type and not any other random type?
languages w/o them feel awkward since you’re generally limited to one statement per line
But you’re also missing one use of the impl keyword: fn func() -> impl Trait.
[...] So dropping the impl in [return position] might not be completely impossible like the other uses of impl.
But the impl markes that it is a trait to the programmers.
Take the following functions:
func1()->A{...}
func2()->A{...}
Does the following snippet compile?
let mut thing = func1();
thing = func2();
Under the current rules we know it will. But if A could be a trait, the functions could return different types. We currently mark that with the impl.
Why? What value does -> () provide? Why not elide that?
What value is provided by keeping it?
What value does cluttering up your code with -> () provide?
Why a syntactic special-case for exactly that type and not any other random type?
Because the unit type is special, just like the never ! type. () also has the special importance of being the return value of an empty statement and some other stuff.
languages w/o [semicolons] feel awkward since you’re generally limited to one statement per line
Over my cold dead body. if-let-else is such a fantastic pattern. Makes everything an order of magnitude more readable. Works so nicely for unwrapping opts/errs.
I really should learn some Rust 😂
Or both? Functional languages are good to be familiar with, but so is Rust's strict ownership model. Both lead to better code in all languages.
It's not only the efficiency boost. What about safe concurrency? Or about the tooling - does sbt really compare favorably to cargo? (I'd say "no", having used both over multiple years)
One mistake they did unfortunately ship though is bind patterns that look like variable names.
There was a recent langdev Stack Exchange question about this very topic. It's a bit trickier to design than it might seem at first.
Suppose we require a keyword -- say
var-- before all binding patterns. This results in having to write things likefor (&(var x1, var y1, var z1), &(var x2, var y2, var z2)) in points.iter().tuple_windows() {},which is quite a bit more verbose than the current
for (&(x1, y1, z1), &(x2, y2, z2)) in points.iter().tuple_windows() {}.Not to mention you'll have to write
let var x = 0;just to declare a variable, unless you redesign the language to allow you to just writevar x = 0(and if you do that, you'll also have to somehow support a coherent way to expressif let Some(x) = arr.pop() {}andlet Some(x) = arr.pop() else {todo!()}).Suppose we require a keyword -- say
const-- before all value-matching patterns that look like variables. Then, what's currentlyturns into either the inconsistently ugly
or the even more verbose
and you always run the risk of forgetting a
constand accidentally binding a new match-all variable namedNone-- the main footgun that syntactically distinguishing binding and value-matching patterns was meant to avoid in the first place.Suppose we require a sigil such as
$before one type of pattern. Probably the best solution in my opinion, but that's one symbol that can no longer be used for other things in a pattern context. Also, if you're already using sigils before variable names for other purposes (I've been sketching out a language where a pointer variable$xcan be auto-dereferenced by writingx), doubling up is really unpleasant....So I can understand why Rust chose to give the same, most concise possible syntax for both binding and value-matching patterns. At least compiler warnings (unused, non-snake-case variables) are there to provide some protection from accidentally turning one into the other.
I went the "only
letintroduces bindings" route, and I'm pretty happy so far:Yeah, they could literally have the same syntax as now, but w/
letwhen introducing a variable. So:Or you could put the
letbefore theSome(...)aslet Some(l), which allows us to keep the currentif let Some(...) = ...syntax. Either of those would feel more consistent than the current implementation.In addition to that, I have my own list of things Rust should not have shipped with, but did.
I'm curious, have you used Rust much? Most of those changes just feel like "rust should be more familiar to me" changes.
Also:
Isn't necessarily true for most of your suggestions. Since most of them are just changes to syntax semantics and not language semantics they could be made in an edition.
I would like to take this a step further: support default arguments (
fn func(a: int = 0); func(); func(a=0)), and then have struct initialization work like that. It's really nice in Python, though I'd like some rules to require args w/ default values to use the assignment syntax, and _require args w/o default values to be in positional order (our codebase is littered withlong_func_name(long_var_name=long_var_name, ...)where function calls are incredibly long and redundant). That does a few things for us:T::new()since now you can just doT()if you just want defaultsfunc(1, 2, some_arg=value)makes it obvious thatvalueis specialT{...}syntax, since you can just use theT(...)syntaxWe probably disagree about requiring positional args to use the assignment syntax, but both your preference and mine can be enforced w/ conventions (i.e. give your variables relevant names to the function).
I disagree, though I can see where you're coming from. Many OOP languages use the first option, and the second try to reuse keywords.
But you're also missing one use of the
implkeyword:fn func() -> impl Trait.I come from Go and I honestly like the syntax there:
Many don't. I like it because it shows that these methods aren't part of the type T, they're "attached" to it. That's much closer to how this actually works that how Java represents it. It's a small thing, but for something low-level like Rust, I think that makes sense.
Also, it makes room for implementing additional functionality on existing types outside of that package. This doesn't work exactly as I'd prefer, but the syntax opens the door to that.
I'm on the fence about this. The
println!("{}", val)syntax is really nice, and that's one of the common uses for varargs. If your varargs are all the same type, you can use slices:fn func(varargs: &[T]); func(&[1, 2, 3])and avoid macros entirely. If your types are inconsistent (i.e. C's varargs), you're going to have a rough time w/ any vararg syntax and will need a macro anyway.I agree that macros shouldn't be abused to emulate varargs, but I don't think we actually need a vararg syntax and can use existing tools.
Agree. Use a built-in like Python's
range(...)instead of fancy syntax, and have the type ([T],&[T], orvec<T>) be inferred at compile time. This can probably just drop-in to everywhere the range syntax is being used currently. It's a little more verbose, but it's way clearer.Are you suggesting using keywords/methods instead? So
[T].slice(...)or evenArray<T>.slice(...)? I think that's reasonable, and it can work similarly toVec, but it would complicate the syntax a bit for my vararg proposal. But yeah,[T, count]is pretty ugly, but I'm not convincedArray<T, count>is all that much better.I think your post could benefit from a couple examples.
I prefer D's
!approach:T!UorT!(U). I would switch macros toT#(...)instead, since#is already used for things adjacent to macros anyway. But to minimize impact, we could just use#for generics instead.I'm not a fan of the
[]for generics, and I disagree w/ Go using that.<>is awkward due to conflicts with comparison operators, so I agree that it needs to go.Eh, I'm less interested in this one, but I don't have a strong opinion. Calling
*T.index(...)isn't a big ask...This is certainly annoying, but surely the right solution is to just make floats implement Eq/Ord, no? I can understand someone preferring the PartialEq/PartialOrd behavior, but that's way more niche than just treating
0and-0as equal andNaNas always unequal.Agreed.
Disagree. I prefer changing it to only do type conversions (i.e. f32 -> f64 is valid, but f64 -> f32 isn't). Value conversions should use a different syntax.
Disagree. Either keep as-is, or put
letnext to the new symbol (if Some(let i) = ...). I don't like chaining, if you want that, just introduce a block or something. It's a bit more verbose, but it's way less confusing.Other languages have destructuring, and this is basically the same thing, and should be handled similarly. For example, in javascript:
let {some: myVar} = option_object;(i.e. object that looks like{some: value}. An option is basically just an object w/ two members, so the syntax should be similar:let Some(my_var) = option_object else default_value;. Throwing anifin front should merely do something like Python's walrus operator,if my_var := some_call(), which evaluates whether the expression is truthy, but in the Rust case, it would evaluate whether the assignment was able to be made.I think if-let makes sense, but don't expand it.
Why? What value does
-> ()provide? Why not elide that?This sounds pretty pedantic. Most of those are quite intuitive, and a lot of this is redundant given the rest of your post.
Disagree. I like that semicolons have meaning in Rust, because in most languages they just feel unnecessary, and languages w/o them feel awkward since you're generally limited to one statement per line.
Thanks for your reply, some replies below!
Not sure, maybe my wording isn't clear enough. What I intended to say is that arguments can be named, not that they have to. In any case, the order of arguments must match the order of parameters, named or not.
That removal could actually happen, so I didn't list it. (Rust started requiring
dynand disallowed naked trait returns with edition 2018. So dropping theimplin that position might not be completely impossible like the other uses ofimpl.)Yes, just methods.
You can have both – that's what's being made possible by them not being in a hierarchy.
It's a bit late for that, isn't it? ;-)
What value is provided by keeping it? Why a syntactic special-case for exactly that type and not any other random type?
Then fixing that might make sense. :-)
But the impl markes that it is a trait to the programmers.
Take the following functions:
Does the following snippet compile?
Under the current rules we know it will. But if A could be a trait, the functions could return different types. We currently mark that with the
impl.What value does cluttering up your code with
-> ()provide?Because the unit type is special, just like the never
!type.()also has the special importance of being the return value of an empty statement and some other stuff.It's fixed with semicolons ;-)
Over my cold dead body. if-let-else is such a fantastic pattern. Makes everything an order of magnitude more readable. Works so nicely for unwrapping opts/errs.
Their suggested replacement is closer to C#'s
iskeyword:The issue here is that we still have let-else, which can't be translated as easily:
I don't think the alternative to let-else is too bad.
That's not an alternative, it's removing
let-elseentirely. It's equivalent to this:let-elseis specifically a feature that allows you to use a refutable pattern to deconstruct a value by providing a divergingelsebranch.Removing
let-elseis the whole point of the linked article series:Being able to do to everything Rust does, but without Rust's zoo of
if-then-else,match,if-let,let-elseetc.The "drop the array and slice syntax" is just nuts. With 0 justification.
@azdle Also https://soc.me/languages/design-mistakes-in-rust for things, I'd rather remove. 🙂