In their list of risks there's one missing: it makes changing the return type of a method to a subclass a subtly breaking change. Say some api defines a method, Collection getNames() { ... } and in your code you do var names = getNames(); if (...) names = Collections.emptySet(); Now, if the api later changes getNames() to be List getNames() { ... } your code breaks. That's one of the advantages of only allowing this…
I think in practice this would be pretty rare. At least coming from a C# background it doesn't seem to be an issue I've ever encountered.
JEP 286: Local-Variable Type Inference
121–130 of 132 posts
Re: JEP 286: Local-Variable Type Inference
#122This 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 (); Which isn't a big deal IMO because the generic parameters had to be specified on the LHS anyway to use the diamond…
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` 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 must be &str).Re: JEP 286: Local-Variable Type Inference
#123Earlier quoted context omitted.
I think in practice this would be pretty rare. At least coming from a C# background it doesn't seem to be an issue I've ever encountered.
It's possible that it's more a theoretical than a practical problem, I would hope so. But it does seem to me like fundamentally a problematic property of a type system that having more accurate types can break your program. It's the static type equivalent of breaking the Liskov substitution principle.
Re: JEP 286: Local-Variable Type Inference
#124Earlier quoted context omitted.
So stop using constructors and start using static factory methods. The return type of a static factory method can be the interface type.
Yeah, let's UML the hell out of everything to be enterprisey and be Martin Fowler-approved! This is what turned off a lot of people before!
So instead of doing this:
Foo foo = new Foo("bar");
You can do this: Foo foo = Foo.of("bar");Re: JEP 286: Local-Variable Type Inference
#125Earlier quoted context omitted.
This is complete nonsense if you're using a static constructor and not a factory. There's zero point to doing this for a local, statically constructed type. It's not a best practice. If you were using a factory, var would infer the factory signature's type, which would normally be the interface.
No, it's not a complete nonsense, no. People abusing Java with overengineering and using factories and other enterprisey design patterns for everything made a lot of people leave the Java land!
Re: JEP 286: Local-Variable Type Inference
#126Earlier quoted context omitted.
No, it's not a complete nonsense, no. People abusing Java with overengineering and using factories and other enterprisey design patterns for everything made a lot of people leave the Java land!
A "static factory" is just a static method. You know, a function. Nothing over-engineered or enterprisey about using functions to allocate objects.
Re: JEP 286: Local-Variable Type Inference
#127Earlier quoted context omitted.
Yeah, let's UML the hell out of everything to be enterprisey and be Martin Fowler-approved! This is what turned off a lot of people before!
Sorry, did "static factory" sound scary? How about "use a function to create your objects" instead? So instead of doing this: Foo foo = new Foo("bar"); You can do this: Foo foo = Foo.of("bar");
Re: JEP 286: Local-Variable Type Inference
#128Earlier quoted context omitted.
A "static factory" is just a static method. You know, a function. Nothing over-engineered or enterprisey about using functions to allocate objects.
Why state the obvious?
So you think using functions is enterprisey and over-engineering things?
Re: JEP 286: Local-Variable Type Inference
#129Earlier quoted context omitted.
This is complete nonsense if you're using a static constructor and not a factory. There's zero point to doing this for a local, statically constructed type. It's not a best practice. If you were using a factory, var would infer the factory signature's type, which would normally be the interface.
No, it's not a complete nonsense, no. People abusing Java with overengineering and using factories and other enterprisey design patterns for everything made a lot of people leave the Java land!
Actually, people left Java land because they couldn't express what they needed to without using complicated design patterns.
Re: JEP 286: Local-Variable Type Inference
#130Earlier quoted context omitted.
Why state the obvious?
> Why state the obvious? So you think using functions is enterprisey and over-engineering things?