Cyan can mix statically-typed and dynamically-typed code, which is called gradual typing. Type Dyn
is a virtual type that is supertype of every other prototype.
// no check is needed here var Dyn d = 0; var Int n; // the compiler inserts a runtime // check: if 'd' does not have type // Int, there is a runtime type error n = d; // runtime type error var Float f; f = d;
In the code below, no type is supplied for the parameters of method search:
. Theirs types are considered to be Dyn
. This code is also here.
package main object Gradual func run { Out println: ( self search: [ 3, 9, 7 ], 7 ); Out println: ( self search: [ "cd", "w", "ab" ], "w" ); var Dyn d = 0; var Int n; n = (d + 1)*5; n println } func search: array, x -> Int { var i = 0; while i < array size { if array[i] == x { return i } ++i } return -1 } end