
Representing the Impractical and Impossible with JDK 10 “var” - pents90
http://benjiweber.co.uk/blog/2018/03/03/representing-the-impractical-and-impossible-with-jdk-10-var/
======
pents90
While I submitted this, I would like to voice my opinion that I am against
"var" in Java. People may ask, "Why should I have to enter in the type if the
compiler can infer it for me?" My answer is twofold: 1) You or some other
maintainer will need to know what that type is later when reading the code. Of
course, "var" is meaningless, requiring you to dig back one or more steps to
determine the actual type. 2) You don't actually need to enter in the type,
any competent IDE can do it for you.

So I'm not sure what we are saving here. When has the time spent typing in
code ever been a bottleneck in software development anyways?

This is my feeling from having worked extensively in Java as well as languages
that support "var": C# and Swift. I feel like my productivity goes down when I
have to support code that uses inferred types. There also seems to be a
performance hit when compiling code with inferred typing, although that may be
circumventable with better compiler tech, who knows.

~~~
matt2000
I agree. It seems very clear that code readability is reduced by var-like type
hiding. When I'm doing code review and I see "var address =
contract.getAddress();" what is the type of that variable? I have no idea.

~~~
trixie_
Address address = contract.getAddress();

Not sure how this helps. I pretty much knew 'getAddress' would return some
sort of Address structure. It's not like knowing the type name tells you what
properties are on it. So what's the point?

~~~
discreteevent
With an IDE (or even most code editors) you can ctrl+click on Address to go
straight to its definition.

~~~
TheDong
`var address` you can also in a short matter of clicks get to the definition
of address's type, whatever it is, if you have an IDE.

~~~
salawat
And what if you don't?

When did programming suddenly shift away from "the programmer should be aware
of what they are doing?"

Hell, I can't count the number of times I've had to troubleshoot bad
imports/classpath management brought about by some genius letting his IDE do
his thinking for him.

~~~
trixie_
Making it harder to program is not synonymous with making the programmer 'more
aware'. I'd argue the opposite. It's much harder to figure out the source of
or find all references of something without an IDE. An IDE with intellisense
makes the programmer much more aware of what his/her options and navigate
large code bases quickly.

String searching text files for definitions or references is just crude.

------
vbezhenar
I'm very disappointed that they didn't make `val` keyword. Very simple change
but code becomes significantly more readable and a lot of bugs will be compile
errors.

~~~
saagarjha
How so?

~~~
rbonvall
I'm sure he means `val` as in Scala, where it is used to define a name that
cannot be rebound:

    
    
        val x = 1
        var y = 2
        x = 3     // compile error
        y = 4     // ok
    

It makes it easier to read code since you don't have to keep track of changes
in your head.

~~~
saagarjha
Yes, this would be a nice addition, but I think final var might be able to
stand in for it as of right now. One issue if it was adopted might be that
“final” in Java doesn’t actually mean a whole lot, since there aren’t a lot of
value types where mutation can be detected.

------
saagarjha
The “impossible” part of the title is an interesting one, since it mirrors how
the arrival of lambdas coincided with the arrival of auto in C++ because such
types are similarly impossible to represent. As I’ve mentioned in another
comment in this thread, I think Java and C++ are very similar with regards for
the need of type inference because they end up having a lot of the same
issues.

------
ScottBurson
This will be nice, but what I really want in Java is a type alias feature
(what C++ calls "typedef"). This would be particularly helpful for function
parameter and return types, which 'var' won't help with.

~~~
dionian
This is one of those things I really liked immediately when switching to
scala. I wonder if java 10 is going to further drive adoption of scala. The
difference in collections API is already argument enough to switch IMHO (hence
I did)

------
Insanity
It's one of the things I really missed when going from C# to Java, but with
IntelliJ, the typing experience is very similar from C#.

Imagine I want to have a variable like:

    
    
         Person p = new Person("John");
    

All I would type in IntelliJ is:

    
    
         new Person("John").var
    

After pressing "tab", that will autocomplete for me and put the focus on the
variable name so I can rename it from the default inferred value.

~~~
vbezhenar
Also you can use "introduce variable" refactoring (ctrl+alt+V in Windows).
Type "new Person("John")" and immediately press ctrl+alt+v.

~~~
Insanity
Indeed, but for me that feels less natural. Matter of taste I suppose :-)

------
rzzzt
If you'd like to try how code looks with the use of this keyword, Lombok has
support for "var":
[https://projectlombok.org/features/experimental/var](https://projectlombok.org/features/experimental/var)

------
walshemj
Can some explain the fist example says infers the type is right there in black
and white

var foo = new ArrayList<String>();

It literally says exactly what foo is !!

~~~
kinghajj
Yes, but in current Java one would have to write

ArrayList<String> foo = new ArrayList<String>()

Or, at least

List<String> foo = new ArrayList<String>()

Whereas with `var`, as you show, the compiler infers the type. Proponents of
this point out exactly what you did: the type is _right there_ , so why should
the programmer have to write it twice?

~~~
stickfigure
Because nobody actually writes this twice.

n,<return>,A,r,L,<return>,S,t,r,<return>,Cmd-Option-V,<return>

The IDE autocompletes everything, extract-to-variable does most of the heavy
lifting.

------
exabrial
My opinion is it you have the time to declare a variable, then you have the
time to hit control-space to put the type. _Not_ putting the type communicates
less information to the next guy. If you "don't care" about the type, then why
are you declaring a variable in the first place? Just chain the call.

------
lkjlakjsdlfkj
"let" is more elegant IMO.

~~~
ajkjk
I always found 'let' to be such a strange name. What on earth is appealing
about it?

The usual variable declaration syntax is <type> <name>; it's not a stretch to
imagine the type as 'var', a catchall type. But 'let'? Let is a verb; it
should be in a place where functions, not types, go. It makes sense in "let x
in {}" type expressions, and it kinda makes sense in Lisp, but I don't see any
argument for it in a C-like language.

~~~
MichaelGG
>It makes sense in "let x in {}" type expressions

Without jumps, all code can be rewritten to be a series of "let x = ... in
<expression>"

The fact Java has statements instead of everything being expressions is a
design flaw and should be rectified not glorified.

~~~
ajkjk
Er. Why do you think that?

~~~
MichaelGG
It's essentially what SSA is.

