by thomastjeffery · 6y ago · 0 comments
Granted, I'm not very familiar with D. I didn't even know it had a borrow checker.
Rust took a lot of great features from Haskell, while keeping itself reasonably approachable from a C/C++/D/Java programmer's perspective.
Rust put type names after variable/function/parameter names, which really cleans up the syntax nicely.
Rust has very nice syntax for pattern matching, monads, and a slew of other really helpful functional language features.
Even if rust didn't have a borrow checker, it would still be a wonderful systems programming language.
Type names after variable or before? When you think about declaring a variable, do you think about its type of name first? I can't imagine you do the latter because "Hmm, I need an array of doubles to represent my tensor" is double[][] tensor; and not tensor: double[][]. Besides the types after names end up being more verbose in the languages which adopt the scheme (hello Scala).
I invite you to get familiar with D syntax to see a wider picture.
The borrow checker in D is WIP and given how much stir it created, I don't see it's happening any time soon.
Also, having "let" at the beginning of the line makes it immediately clear that it's a variable definition, whereas if it starts with some arbitrary type I haven't seen before I have to spend more time figuring out what's happening. Good syntax highlighting helps, but I think starting with types like C/C++/D is less elegant than starting off with a keyword that clearly identifies what the line does.
Similar applies for function definitions. It's nice when the function names are all lined up and not buried halfway across the screen because it has some long return type.
double[] arr;
bam! Simple and clear.
What about functions? It has input, name and output and D nicely presents you with
double[] createArray(int size) {...}
This is as little typing as you can get while keeping readability.
And here is a basic Rust function
fn add_one(x: i32) -> i32 {...}
What kind of type is i32 btw? oh, there is u32 as well. integer 32bit and unsigned 32bit? What on earth are these? Ah, this is just D's size_t, the same size_t as in C/C++. So that C/C++ ppl have zero overhead reading D code and for Rust they will google. To top that, you have also -> because of the type after the definition scheme. Needless verbosity, same thing in Scala.
What about Rust unable to drop parenthesis after a function?
self.host_addr.clone()
Should I mention Rust's UFCS?
But I am nitpicking, right. This doesn't make a language bad in any way. However, as I said, Rust readability is pretty subpar in addition to already steep language learning curve.
Also, I find i32 and u32 types much cleaner than D's "unsigned int" and "int". What is the difference between "unsigned int" and "unsigned long" in D? It's still better than C++, but I'd rather write i32 and i64 rather than "int" and "long".
Usually, the reason is to make parsing easier. C++ is infamous for having undecidable grammar. Ruby is extremely pleasing to the eye, but its grammar is a clusterfuck. Newer languages have learned from that, so they try to have simpler grammars.
Another reason for those small syntax markers is to avoid implicit language decisions. In Rust, having to write one of "let/let mut/const" means forcing the programmer to make an explicit choice regarding mutability and memory usage. In Scala, "val/lazy val/var/lazy var/def" means having to think about mutability and evaluation times.
> What about Rust unable to drop parenthesis after a function?
In the case of Rust, the reason is uniformity. Even Scala, of all languages, is walking back on the optional parentheses, because the language allows to define functions with multiple argument lists, and optional parentheses introduce ambiguity between functions with empty argument lists and functions with no argument lists.
Rust has usize [0] as a size_t equivalent.
Since I am not familiar with Scalia, I am not sure why the types after the name would end up being more verbose, as far as I can tell the only difference is the : between the variable and its type. Would you mind expanding on that?
Personally although Rust definitely does not have the elegance of Haskell or Python, I definitely would not call it ugly. There aren’t that many symbols and once you actually spend the time to learn the language the semantics for each syntax element are pretty easy to remember. I also have looked at D, but the lack of algebraic data types makes it a non-starter for me.
In any case, as a Scala dev, I wish the compile times were as short as in Rust!
I completely disagree with your opinion on this subject, though I recognize that it's mostly subjective. I think types after the variable are completely better except for my muscle memory from C.
You can just as equally think of it as, "I need my tensor to be an array of doubles".
That works much more cleanly with type inference, because the type is more of an annotation, and you can drop it without adding the boilerplate word "auto".
> Subjectively ugly and polluted with punctuation symbols that tell you little about their semantics.
Like what?
I think this is where Rust falls apart. In things like C, more features requires writing more C code. In Rust, more features requires learning more syntax features.
In the tutorial, the language looks quite nice, comparable to Python. But in production level code, there are {.pragmas.} in a lot of places and they are not just advice for the compiler that can be ignored: they are important keywords that don't pollute the identifier space.
Another beef I had is that slice ranges and for loops are closed intervals by default. This is contrary to both Python and C, and while there is a syntax a..<b to be open on the right, it seems like a bad decision to be different from C and Python by default. It reminded me of Julia arrays starting at 1 by default. I guess I can see that if your audience is Fortran programmers, but arrays haven't started at 1 in a new language for a long time afaik.
I wish Nim had a goal of being a typed, compiled Python. But I don't think that ever was a main goal and now it's sort of too late because the language has a very wide scope (my opinion) and has compatibility concerns with all of its previous design choices.
My opinion is generally the opposite, that simple decisions like putting type annotations after names, etc. make it so each unavoidable complexity in syntax has its own home, instead of piling together like C++/Java/etc.
As a programmer who first used Python than C++, I don't find types after or before any nicer or worse.
As for Monads, you could write a template that wraps a pure function that creates side effects through its arguments into a Monad rather easily.
As for pattern matching, you can do that by writing your own template too.