Jump to content

Java might be getting local-variable type inference

madknight3

Many languages already have local-variable type inference and it looks like Java may finally be getting there too. JEP 286 has the proposal details. There's also a survey you can take to go with it to give them some feedback about the proposal. One thing the survey asks about is the preferred syntax for the feature. Options are

//A: var only (like C#)

var lib = getMusicLibrary();
var artists = lib.getArtists();
var personCount = artists.size();
var works = lib.getTracks();
var workCount = works.size();

//B: var/val (like Scala, Kotlin)

val lib = getMusicLibrary();
val artists = lib.getArtists();
var personCount = artists.size();
val works = lib.getTracks();
var workCount = works.size();

//C: var/let (like Swift)

let lib = getMusicLibrary();
let artists = lib.getArtists();
var personCount = artists.size();
let works = lib.getTracks();
var workCount = works.size();

//D: let only

let lib = getMusicLibrary();
let artists = lib.getArtists();
let personCount = artists.size();
let works = lib.getTracks();
let workCount = works.size();

//E: auto only (like C++)

auto lib = getMusicLibrary();
auto artists = lib.getArtists();
auto personCount = artists.size();
auto works = lib.getTracks();
auto workCount = works.size();

 

It's small, and there's still a lot more that can be done to reduce boilerplate, but I expect many people who use Java will support this proposal (and as always, many people will probably hate it for some reason too).

Link to comment
Share on other sites

Link to post
Share on other sites

This is exciting! Put my vote in for the C# var style. Close second would probably be the var/let style, but in my time with Swift I constantly think I'd rather there just be a var keyword rather than var/let.

 

Thanks for sharing!

Link to comment
Share on other sites

Link to post
Share on other sites

auto ftw.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, jslowik said:

This is exciting! Put my vote in for the C# var style. Close second would probably be the var/let style, but in my time with Swift I constantly think I'd rather there just be a var keyword rather than var/let.

 

Thanks for sharing!

I think the var/val combo would be my first choice. Given that both Scala and Kotlin are on the JVM, it seems nice to have them all be consistent.

 

None of the options seem that bad to me though so I'd be fine with whatever.

 

7 hours ago, Nineshadow said:

auto ftw.

Except auto, that's just weird. Plus it has 4 characters instead of 3. Clearly inferior.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×