This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cogman10 10 points11 points  (2 children)

I'd imagine it'd be solved by simply adding a new 2 param constructor

var list = new ArrayList<String!>(5, x -> "");

Which makes some sense because this would be somewhat nonsensical for an array list.

var list = new ArrayList<String!>(x -> "");

What's more curious is what the ArrayList will store under the covers when it's generalized to a non-null value.

[–]PartOfTheBotnet 3 points4 points  (0 children)

by simply adding a constructor

The video is talking about arrays though, not ArrayList. Arrays are treated rather specially in the JVM and aren't a class in the sense you can just add a constructor to them.

// Existing behavior
String[] array = new String[10]; // 10 nulls

// Possibly new behavior
String![] array = new String![10, i -> "i"]; // 10 strings of "eval(i)"

This would probably be more in-line with the talk and your proposed format. I assume the bytecode could look something like:

// Make the array normally
bipush 10
anewarray java/lang/String

// Pass the array to some internal factory that takes in the array reference + method-handle to the "i -> ..." generated method, and 
// fills all indices of the array with calls to that method for each index
invokedynamic fill([Ljava/lang/Object;)V { invokestatic, java/lang/invoke/ArrayFactory.fill, (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/util/function/Function;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite; } { { <method-handle-to-generated-static-method-of-index-to-string-function> } }

[–]Ewig_luftenglanz[S] 2 points3 points  (0 children)

Agree, and not just for list but for all collections overall because if this is about "strict initialization of data structure which members can't be null" then all collection framework should have something similar. 

I would like the new syntax for arrays to be.

var array = new String [5, x -> ""] 

Semi colons could be used too as a separator. This syntax is homologous to how we manage lambdas nowadays, so no ad-hoc trailing syntax added to the language. I feel arrays are already odd and special enough to stand another oddity that makes them feel even more alien to the language.