Types

Basically all kinds of JVM types are available and referable.

Arrays

Arrays cannot be declared explicitly, but they can be passed around and if needed are transparently converted to a List of the compound type.

In other words, the return type of a Java method that returns an array of ints (int[]) can be directly assigned to a variable of type java.util.List<java.lang.Integer> (in short List<Integer>). Due to type inference you can also defer the conversion. The conversion is bi-directional so any method, that takes an array as argument can be invoked with a List instead.

Simple Type References

A simple type reference only consists of a qualified name. A qualified name is a name made up of identifiers which are separated by a dot (like in Java).

Syntax

QualifiedName:
  ID ('.' ID)*
;    

There is no parser rule for a simple type reference, as it is expressed as a parameterized type references without parameters.

Examples

Function Types

Xbase introduces closures, and therefore an additional function type signature. On the JVM-Level a closure (or more generally any function object) is just an instance of one of the types in org.eclipse.xtext.xbase.lib.Function*, depending on the number of arguments. However, as closures are a very important language feature, a special sugared syntax for function types has been introduced. So instead of writing Function1<String,Boolean> one can write (String)=>Boolean.

Primitives cannot be used in function types.

For more information on closures see section Xbase_Expressions_Closures.

Syntax

XFunctionTypeRef:
  ('('JvmTypeReference (',' JvmTypeReference)*')')? 
      '=>' JvmTypeReference;

Examples

Parameterized Type References

The general syntax for type references allows to take any number of type arguments. The semantics as well as the syntax is almost the same as in Java, so please refer to the third edition of the Java Language Specification.

The only difference is that in Xbase a type reference can also be a function type. In the following the full syntax of type references is shown, including function types and type arguments.

Syntax

JvmTypeReference:
  JvmParameterizedTypeReference | 
  XFunctionTypeRef;
    
XFunctionTypeRef:
  ('(' JvmTypeReference (',' JvmTypeReference)* ')')? 
      '=>' JvmTypeReference;

JvmParameterizedTypeReference:
  type=QualifiedName ('<' JvmTypeArgument (',' JvmTypeArgument)* '>')?;

JvmTypeArgument:
  JvmReferenceTypeArgument | 
  JvmWildcardTypeArgument;
  
JvmReferenceTypeArgument :
  JvmTypeReference;

JvmWildcardTypeArgument:
  '?' (JvmUpperBound | JvmLowerBound)?;

JvmLowerBound :
 'super' JvmTypeReference;

JvmUpperBound : 
 'extends' JvmTypeReference;

Examples

Primitives

Xbase supports all Java primitives. The conformance rules (e.g. boxing unboxing) are also exactly like defined in the Java Language Specification.

Conformance and Conversion

Conformance is used in order to find out whether some expression can be used in a certain situation. For instance when assigning a value to a variable, the type of the right hand expression needs to conform to the type of the variable.

As Xbase implements the unchanegd type system of Java it also fully supports the conformance rules defined in The Java Language Specification.

Common Super Type

Because of type inference Xbase sometimes needs to compute the most common super type of a given set of types.

For a set [T1,T2,...Tn] of types the common super type is computed by using the linear type inheritance sequence of T1 and is iterated until one type conforms to each T2,..,Tn. The linear type inheritance sequence of T1 is computed by ordering all types which are part if the type hierarchy of T1 by their specificity. A type T1 is considered more specific than T2 if T1 is a subtype of T2. Any types with equal specificity will be sorted by the maximal distance to the originating subtype. CharSequence has distance 2 to StringBuilder because the supertype AbstractStringBuilder implements the interface, too. Even if StringBuilder implements CharSequence directly, the interface gets distance 2 in the ordering because it is not the most general class in the type hierarchy that implements the interface. If the distances for two classes are the same in the hierarchy, their qualified name is used as the compare-key to ensure deterministic results.