4 ms·
Many languages allow you to elide the type, which is another nice thing about the type following the identifier. In Scala, in particular, types are not the ass
by andrewla 7y ago
Many languages allow you to elide the type, which is another nice thing about the type following the identifier.
In Scala, in particular, types are not the assigned type like in C (where they also serve as the storage specification) -- they are assertions, that the compiler will check are compatible with the code.
So `val x: int = "hello"` is no good and the compiler can cut it short right there; this is especially useful as call-site documentation.
- throwanem 7y agoConversely, a lot of languages will infer type from first assignment, so in e.g. TypeScript "let x = 5", x is inferentially typed as 'number' and the type checker will throw if the implicit constraint is later violated. This reduces the need for explicit type annotations, clearing up a lot of the visual and cognitive noise.
- dastbe 7y agoThere is still a distinction between primitives and object types in Scala, so it's not correct to say that types in Scala aren't used for storage specification. It's also the case that there are plenty of languages where all types imply storage specification and they support plenty of type elision.