Java-TX

Content


Introduction

Java-TX (Java Type eXtended) is an extension of Java in which a global type inference algorithm and real function types are added. Since the end of the nineties features from functional programing languages have been transferred to Java. Parametric polymorphism extended by wildcards, called generics, were transfered to Java 5.0. Higher-order functions and lambda expression were introduced in Java 8. Java 8 uses functional interfaces as target types of lambda expressions in contrast to real function types as in functional programming languages. The powerful feature type inference from functional programming languages is incorporated into Java, as into other object-oriented languages, i.e. only in a restricted way called local type inference. Local type inference allows certain type annotations to be omitted. For instance, it is often not necessary to specify the type of a variable. Type parameters of classes in the new-statement can be left out. Return types of methods can often also be omitted. Local type inference is at its most pronounced in Scala. In Java 10 an extention of local type inference is introduced, where types of local variables can be replaced by the keyword var and inferred automatically during the compilation. In contrast to global type inference, local type inference allows types of recursive methods and lambda expressions not to be omitted.
The Java-TX project contributes to the design of object-oriented languages by developing global type inference algorithms for Java-like languages.

First Example

The class Id has the method id. The type annotations are omitted.
 
  class Id {
      id(x) {
          return x;
      }
  } 
 
The type inference algorithm inferrs the types, such that Id can be applied:
    new Id().id(1);

    new Id().id("hallo");

More complex example

  import java.lang.Integer;
  import java.lang.Double; 
  import java.lang.String; 


  class OL {
     m(x) { return x + x; }
	
  }
    
         
  class OLMain {
     main(x) { 
	var ol;
	ol = new OL(); 
	return ol.m(x); 
     }  
  }
The type inference mechanism considers only imported types. Therefore Integer, Double, and String are imported.
As the operator + is overloaded by all numeric types and String the methods m in the class OL and main in the class OLMain, respectively, gets all these types. The generated classfile demonstrates this:
> javap OL.class 
Compiled from "OL.jav"
class OL {
  public OL();
  public java.lang.Integer m(java.lang.Integer);
  public java.lang.Double m(java.lang.Double);
}

> javap OLMain.class 
Compiled from "OLMain.jav"
class OLMain {
  public OLMain();
  public java.lang.Integer main(java.lang.Integer);
  public java.lang.Double main(java.lang.Double);
}

Last modified: Thu Jul 9 15:26:27 CEST 2020