User Tools

Site Tools


dev-guide:automatic-conversion

Implicit conversion

GroIMP compiler can automatically perform implicit conversions. By default RGG files use this implicit conversions. A common example is the conversion from float to double (and vice versa).

Add a conversion

During compilation, if a type if forced onto another (and they are not compatible), the compiler try the Autoconversion, which includes widening conversions, boxing and unboxing, and allows to transform an object of type A into an object of type B by means of conversion functions. Every function declared as 'static B valueOf(A)', 'static B toB(A)', 'B A.toB()' and constructors of the form 'B(A)' are considered as conversion functions. If more than one conversion from type A to type B using those conversion functions is possible, the conversion is ambiguous and results in an error.

Example

This is a working piece of code in RGG, that cannot compile in java:

class Test1 {}
 
class Test2 {
  static Test2 valueOf(Test1 var){
    return new Test2();
  }
}
 
public run() {
 Test1 t1 = new Test1();
 Test2 t2 = t1;  // in java this would result in an error. t1 cannot be automatically casted to Test2.
}
dev-guide/automatic-conversion.txt · Last modified: 2025/01/08 16:30 by gaetan