Skip to main content

classes are dead, long live to lambdas

Posted by forax on April 1, 2013 at 7:10 AM PDT

I'm very proud to announce that in order to be fully compatible with the new multi-core era,
we (the lambda expert group) have decided to deprecate the class syntax in Java 8
and to remove it in Java 9.

With the introduction of lambda, classes are not needed anymore and are now seen
as stimagata from the past.

so instead of writing a class User like this:

public class User {
  private final String firstName, lastName;

  public User(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public String firstName() {
    return firstName;
  }
 
  public String lastName() {
    return lastName;
  }
 
  public static void main(String[] args) {
    User bob = new User("Bob", "Vance");
    System.out.println(bob.firstName());
    System.out.println(bob.lastName());
  }
}

the new canon is:

public interface User {
  public static User User(String firstName, String lastName) {
    return ref -> (ref == 0)? firstName: lastName;
  }

  public default String firstName() {
    return transform(0);
  }
 
  public default String lastName() {
    return transform(1);
  }
 
  public abstract String transform(int index); 
 
  public static void main(String[] args) {
    User bob = User("Bob", "Vance");
    System.out.println(bob.firstName());
    System.out.println(bob.lastName());
  }
}

Note that the code above currently compile with the latest beta of jdk8
but the compiler fails to translate the lambda correctly so
it doesn't work at runtime. This should be fixed in the next update.

Anyway, there are several advantages of this design compared to the old one,

  • object are always non-mutable
  • object classes are created only when needed
  • empty object (NullObject) are automatically true constant
  • object can only be referenced by their interfaces
  • constructor are not written directly so half baked objects can not be observed
  • GC algorithms are simple. No write barrier, no card marking because
    reference are always from new object to old ones.

Welcome to a new era,
classes are dead, long live to lambda ...

Related Topics >>