Today we had a second bgjug hackaton for the month. Its topic was Valhalla.
Project Valhalla will probably be part of Java 10 (expected 2018). Among other things it allows using primitives as generic arguments.
Here's how you can build Valhalla yourself.
Here's a virtual machine with valhalla if you're lazy (virtual box).
This is the valhalla presentation by Ivan. And this is the video of the talk (in Bulgarian).
Here are some of the code we ran:
Ivan created a simple ArrayList
public class SpecialArrayList { private static int INITIAL_SIZE = 10; private T[] elements = null; private int length = 0; public SpecialArrayList() { elements = new T[INITIAL_SIZE]; } public T get(int position) { if(position >= length) { throw new ArrayIndexOutOfBoundsException(); } return elements[position]; } public void add(T element) { // Check whether array is full elements[length] = element; length++; } } public class Main { public static void main(String[] args) { SpecialArrayList intList = new SpecialArrayList<>(); intList.add(1);intList.add(2);intList.add(3);intList.add(4); System.out.println(intList.get(2)); SpecialArrayList stringList = new SpecialArrayList<>(); stringList.add("1");stringList.add("2");stringList.add("3");stringList.add("4"); System.out.println(stringList.get(3)); } } |
Nayden tested a bit of crazy code - which is supposed to fail (IA impl = new IAImpl();):
public class B { protected T t; protected T[] ts; public B(){} public B(T t){ this.t = t; this.ts = new T[100]; // System.out.println(this.t.getClass()); } public T[] getTs(){ return this.ts; } public T getT(){ return this.t; } public static void main(String[] args){ B b = new B<>(100); System.out.println(b.getT()); System.out.println(b.getClass()); System.out.println((new B("dada")).getClass()); System.out.println(c.getClass()); IAImpl ludnica = new IAImpl(); ludnica.setT(100); System.out.println(ludnica.getT() instanceof Object); IABase base = new IAChild(); base.setT("dada"); System.out.println(base.getT()); System.out.println(base.getClass()); IA impl = new IAImpl(); impl.setT("dada"); System.out.println(impl.getT()); System.out.println(impl.getClass()); } } interface IA{ public T getT(); public void setT(T t); } class IABase{ private T t; public T getT(){ return this.t; } public void setT(T t){ this.t = t; } } class IAChild extends IABase { } class IAImpl implements IA { private Object t; public Object getT(){ return this.t; } public void setT(Object t){ this.t = t; } } |
Hi Ivan, BGJUG,
Nice blog post, I have only caught up with my OpenJDK mails.
Could you please post the output of this program on your post - it can help others verify if they are doing it right.
Good work - I'll add these and other links to your Adopt OpenJDK book.
Cheers,
Mani
I don't have valhalla right now to post the result
Oops! Its you Mihail who wrote this post 😉