3 ms·
New to Rust, was excited to see Prolog anonymous variables make an appearance. But one thing struck me is the mismatch between variable and function declaration
by infradig 6y ago
New to Rust, was excited to see Prolog anonymous variables make an appearance. But one thing struck me is the mismatch between variable and function declarations...
let x: i32 = 42;
vs
fn fair_dice_roll() -> i32 { 4 }
It seems the designers missed an opportunity, as the latter could quite easily have been:
fn fair_dice_roll() : i32 = 4;
or
fn fair_dice_roll() : i32 = { ... code that produces 4... }
at the slight expense of giving up the cutesy right-arrow. Maybe i'm just used to Prolog/Lisp code=data syntactic simplicity.
And that's as far as I got as I exited the page scratching my head to write this comment.
- gusfrehse 6y agoThe way I see it, that would mean the function 'fair_dice' has type 'i32'. While in this case it maybe fine, if the function had arguments, it would be ambiguous.
- kag0 6y agoI thought the same. After how consistent Scala's syntax is, it's hard to adjust to what Rust has done here. In Scala val hello : String = "hello" // eagerly evaluated once def hello : String = "hello" // evaluated every time def hello(name: String) : String = s"hello $name" // parameters
- swsieber 6y agoWith type ascription being optional in some places, I have to wonder if using the colon for both type ascription and fn return type would introduce any parsing/grammar ambiguities. I personally like the arrow being used for return types as it highlights pretty well what returns stuff. I don't need to track fn definitions or anything when scanning a file - I just need to look for the arrows.