3 ms·
Type inference in newer languages reduces a lot of the pain that was Java. Of course, it’s not new but adoption took some time I also like to explicitly decla
by melling 6y ago
Type inference in newer languages reduces a lot of the pain that was Java.
Of course, it’s not new but adoption took some time
I also like to explicitly declare immutability/ mutability
let x = “hello world”
var ctr = 0
- asn0 6y agoSince Java 10: var mutableVar = "Foo" final var immutableVar = "Bar"
- dtech 6y agoFinal has been in Java for a lot longer, but you're also missing the point. Immutability should be the default, or at least not a secondary citizen to the dangerous and often unnecessary mutability.
- imtringued 6y agoYes, I still remember how in Ceylon they just did "value x = bla" and the variable was immutable by default. If you wanted to actually make it mutable you had to write it out as "variable Type x = a" to make it stand out and be really annoying to have mutable variables.
- kaba0 6y agoWhich is the road taken by record classes and the upcoming primitives.
- cloogshicer 6y agoThis is not immutability, final only prevents re-assignment, not mutation: final ArrayList<String> foo = new ArrayList<>(); foo.add("Foo"); // Compiles and runs - this is mutation foo = new ArrayList<>(); // Compiler error - this is re-assignment In your example, both Strings are immutable, since Java Strings are always immutable.
- imtringued 6y agoThe variable is immutable.
- MaxBarraclough 6y ago> I also like to explicitly declare immutability/ mutability Scala takes this approach, using var for declaring mutable variables, and val for declaring immutables. In my experience, in well designed imperative code, only a small proportion of variables need mutation, so using keywords like const and final is backward. It would make more sense to use a mutable keyword, or to do as Scala does. (C++ has a mutable keyword, but it's for a different purpose.) https://docs.scala-lang.org/overviews/scala-book/two-types-variables.html https://docs.scala-lang.org/overviews/scala-book/two-types-v...
- tandr 6y agoUse lombok "addon"? https://projectlombok.org/features/val https://projectlombok.org/features/val
- aranchelk 6y agoExactly what I was thinking, tedious type annotations on everything vs dynamic types, we have languages with strong static type systems and inference, so it’s a false choice.