11 Type inference
When writing programs in Gont, you will soon note, that type expressions
can get quite large. To save your keyboard Gont compiler can guess types of
variables for you. You can instruct it to do so, by using underscore
(_
) instead of type. Example:
_ s = "Hello world"; // string s = "Hello world";
_ make_tuple = fun _ (_ x, _ y) ([x, y]);
_ make_tuple2 = fun (x, y) ([x, y]);
make_tuple2
definition is identical to make_tuple
. If all
types in lambda expression are _
, then you can omit them all.
11.1 It's guessing? Is it safe?
Well... guessing isn't best description of type inference. It simply knows.
If there are any problems, it will report them. For example in:
fun (x, y) (x + y)
type of x
and y
can be
float
, int
or string
. Therefore Gont compiler
will reject it. However it won't complain about
fun _ (int x, _ y) (x + y)
.
11.2 Caveats
Current algorithm is somewhat limited, it will lose given complex
enough example. _
is not allowed in global symbols. Both
make_tuple
and make_tuple2
above are weakly polimorphic.