6 ms·
This is pretty nice. Hope it goes through. It's about time that we had local-variable inference in Java. It does mean that the diamond can't be used this way:
by vivin 11y ago
This is pretty nice. Hope it goes through. It's about time that we had local-variable inference in Java. It does mean that the diamond can't be used this way:
var list = new ArrayList<>();
There is no way to infer the type of the generic parameter. So we will go back to doing:
var list = new ArrayList<String>();
Which isn't a big deal IMO because the generic parameters had to be specified on the LHS anyway to use the diamond. You also end up using fewer characters in general with var even without being able to use the diamond.
Regarding val vs let, I prefer let simply because it's harder to accidentally type let vs typing var instead of val (or vice versa).
- namelezz 11y agoI hope this gets rejected. The given examples are too ideal as they are all using familiar Java standard APIs. I doubt if it's a good idea in practice since you can end up with unreadable code when integrating with third party APIs.
- msbarnett 11y ago> I doubt if it's a good idea in practice since you can end up with unreadable code when integrating with third party APIs. This works perfectly fine in practice as can be seen using comparable constructs in C#, C++, and many other mainstream languages. If anything it's more readable, since your code isn't littered with redundant type info.
- namelezz 11y ago> If anything it's more readable, since your code isn't littered with redundant type info. I fail to see the redundant type info. Example 1: int maxWeight = blocks.stream() .filter(b -> b.getColor() == BLUE) .mapToInt(Block::getWeight) .max(); "int" is not redundant type info here. Example 2: List<String> list = new ArrayList<>(); // (1) var list = new ArrayList<String>(); // (2) Many would say List<String> in (1) is redundant but I would argue otherwise. The intention of List<String> in (1) is to reference to the underline ArrayList through List interface. (1) is trying to encourage programming to an interface while (2) just completely destroys the practice by inferring the type for list variable to be ArrayList<String>. That means subsequent method invocations after (2) can be methods from ArrayList not List.
- haffla 11y agoYou fail to see that type inference is optional. So yes for readability's sake, in your first example I wouldn't omit the "int", but in simpler cases you CAN omit if you want. And that's just nice, end of story.
- nikolay 11y agoOptional, alright, but promoting bad practices. If people like dumb-down languages, there are plenty of them already!
- oldmanjay 11y agoWhat are the bad practices being promoted by this? How does it qualify as dumbed down? Simply ranting about things that, charitably, you don't seem to understand, does not make the rants valid.
- kodablah 11y ago> Regarding val vs let I like the approach mentioned of just reusing final for immutability.
- cballard 11y agoMaking the immutable version the same length as (or preferably, shorter than, e.g. Rust) the mutable version encourages the use of immutability.
- skippytheroo 11y agoval is a much better idea. Final is longer to type, in many cases as long as just declaring the type. We should be encouraging immutability not discouraging it by making it more verbose than necessary.
- arrty88 11y agoI've been doing List<String> list = new ArrayList<>(); Isn't that good enough?
- hyperpape 11y agoNo. The List<String> is almost as redundant as the RHS <String> was.
- the_af 11y agoFor your example, it makes no difference. However, take a look at the other example mentioned at the beginning: var stream = list.stream(); // infers Stream<String> How would you write it in order to take advantage of type inference?
- Others 11y agoAs far as val vs let goes, I actually prefer val, because it has symmetry with var. That is to say "variable foo" is more similar to "value foo" than "let foo," since "value" is an adjective like "variable". Of course, I suppose this is personal preference.
- rat87 11y agothere is but I don't know if it can be retrofitted onto java. In rust and haskell, ect. types can be worked out backwards so that the type is figure out with use. fn main() { let mut v = Vec::new(); v.push("hello"); v.push("world!"); println!("output: {:?}", v.join(" ")); } the type of v is `Vec<&str>` worked out backwards from where &str literal is added(since the type of push is fn `push(&mut self, value: T)` the T generic of Vec<T> must be &str).