User Tools

Site Tools


05_developer_tutorials:dev-guide:automatic-conversion

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
05_developer_tutorials:dev-guide:automatic-conversion [2025/02/19 15:51] – removed - external edit (Unknown date) 127.0.0.105_developer_tutorials:dev-guide:automatic-conversion [2025/02/19 15:51] (current) – ↷ Page moved from 04_developer_documentation:dev-guide:automatic-conversion to 05_developer_tutorials:dev-guide:automatic-conversion gaetan
Line 1: Line 1:
 +====== 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:
 +
 +<code 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.
 +}
 +</code>
 +