4 ms·
I agree, though I could see the merit for a standalone declaration: val x: String x = "hello" The type at this point is almost like a comment. For declar
by lliamander 7y ago
I agree, though I could see the merit for a standalone declaration:
val x: String
x = "hello"
The type at this point is almost like a comment.
For declaration and assignment though, I agree that reading "ident: Type" is harder for me.
Perhaps an interesting idea would be to have the type at the end of the expression. Like so:
val x = "hello": String
Essentially, you're making a type assertion on an expression. Since it's an assignment expression (the value of which would be the assigned variable) then it also type checks the variable.
- djur 7y agoMost statically typed languages don't even need the type assertion in a case like this, though. A literal has a definite type (hopefully), so the type of x can be inferred. val x = "hello" Standalone declarations are the most important problem to solve here.
- lliamander 7y agoYeah, type inference is preferred (and pretty common). I'm just saying that, if I had (or wanted to) set the type, at the end of the expression would be my preferred place.
- hinkley 7y agoBut now it's difficult to declare that x is single assignment.
- lliamander 7y agoNot sure that's true. I'd I say: let x = "hello": String let x = "world": String If 'let' is a keyword that implies single assignment, then problem is solved. Alternatively, using pattern matching alarm Erlang would be even better: x = "hello": String //x is bound to "hello" x = "hello": String //equality check is successful x = "world": String //Error: values not equal
- Sharlin 7y ago> Perhaps an interesting idea would be to have the type at the end of the expression. Like so: > val x = "hello": String Rust has had exactly this (so-called type ascription) for a while in nightly, but it's not stabilized yet due to some unresolved issues.
- lliamander 7y agoGood to know!