Live data from Hacker News

Representing the Impractical and Impossible with JDK 10 “var”

benjiweber.co.uk

101–110 of 131 posts

Re: Representing the Impractical and Impossible with JDK 10 “var”

#101
post #98

Earlier quoted context omitted.

I'm sorry but you are plain wrong. var transactions = account.getTransactions(); ...tells you nothing. Is transactions iterable? Is it an enum? Is it ordered? Are duplicates allowed? Maybe you're returning a basic type? Maybe it's another pojo?

The type definition alone wouldn't tell you if it's an enum, ordered, or no duplicates allowed... The writer would know what type they're working with through intellisense. The reader would know it's iterable based on just reading the code that follows it.. So again, what does the type info give you? Most of what you mentioned you wouldn't be able to figure out from just a type name...

List tells me it's an ordered collection of a singular superclass that allows duplicate instances.

var tells me nothing.

Re: Representing the Impractical and Impossible with JDK 10 “var”

#102

Earlier quoted context omitted.

Counter-argument to changing the return type of a function with var: You can change the return type of the function to another type that might break some assumptions later in the code. For example, if the code assumes it has a type Foo with a length field, and you change the return type of tha function to a Bar without that length field, the compiler will complain that Bar doesn't have a length field, rather than com…

In your example, after you manually change the return type at the caller to Bar from Foo, the usage of Foo.length and Bar.length remains and nothing will help you if they changed semantics with neither inferred nor explicit types. The access of the length field is still valid in either case.

[deleted]

Re: Representing the Impractical and Impossible with JDK 10 “var”

#103
post #16

Earlier quoted context omitted.

I'm pro var * I write code a lot more fluidly with var. When I go back to writing non-var code (enforced by some departments) I find that it breaks my focus on solving the problem at hand. I end up writing my code with var and then going back and replacing my vars with the type names. * I find code a lot easier to read. I can understand the flow of the logic easier, the variable names are enough. Unless you have the…

I think my productivity [temporarily] goes up, but my coworkers' productivity [always] goes down, when using dynamic and untyped stuff.

var is neither dynamic nor untyped

Re: Representing the Impractical and Impossible with JDK 10 “var”

#104
post #98

Earlier quoted context omitted.

The type definition alone wouldn't tell you if it's an enum, ordered, or no duplicates allowed... The writer would know what type they're working with through intellisense. The reader would know it's iterable based on just reading the code that follows it.. So again, what does the type info give you? Most of what you mentioned you wouldn't be able to figure out from just a type name...

List tells me it's an ordered collection of a singular superclass that allows duplicate instances. var tells me nothing.

'var transactions' is enough information for most people to understand it's a list of some sort. If it really matters to you then use the IDE. The full type info is just not that useful. That's why most languages are moving towards inferred typing - typescript, c#, java, c++, rust, go, scala, etc...

Re: Representing the Impractical and Impossible with JDK 10 “var”

#106

Earlier quoted context omitted.

List tells me it's an ordered collection of a singular superclass that allows duplicate instances. var tells me nothing.

'var transactions' is enough information for most people to understand it's a list of some sort. If it really matters to you then use the IDE. The full type info is just not that useful. That's why most languages are moving towards inferred typing - typescript, c#, java, c++, rust, go, scala, etc...

How do you know it's not a count of the transactions?

Re: Representing the Impractical and Impossible with JDK 10 “var”

#107

Earlier quoted context omitted.

List tells me it's an ordered collection of a singular superclass that allows duplicate instances. var tells me nothing.

'var transactions' is enough information for most people to understand it's a list of some sort. If it really matters to you then use the IDE. The full type info is just not that useful. That's why most languages are moving towards inferred typing - typescript, c#, java, c++, rust, go, scala, etc...

And the fact we're (amicably) disagreeing just proves I'm right. List leaves no ambiguity

Re: Representing the Impractical and Impossible with JDK 10 “var”

#108
post #25

Earlier quoted context omitted.

> You or some other maintainer will need to know what that type is later when reading the code. The Microsoft rule is to only use var when the type is obvious from the assignment. That essentially boils down to new, cast and anonymous types.

That seems to go against the OP's example benefits of very complex types or actually impossible types. The former will not be obvious from the assignment by nature of their complexity, no? The latter... well, being impossible without `var` implicit types is probably often also not obvious.

The "impossible type" (which is just anonymous) occurs within the same statement, so it is obvious.

Re: Representing the Impractical and Impossible with JDK 10 “var”

#109

Earlier quoted context omitted.

let as a drop-in replacement for var, or something semantically different?

Generally in languages that have a let, it means const var.

In JavaScript it defines a local variable or something like that, so there’s that…
Post reply on HN