enso/docs/syntax/conversions.md
Jaroslav Tulach 2964457d48
Intersection types & type checks (#11600)
Implementation of **type checks** for **intersection types**. The idea is to split the list of `types[]` in `EnsoMultiValue` into two parts:
- first `methodDispatchTypes` represent the types the value _"has been cast to"_
- the rest of the types represent the types the value _"can be cast to"_

By performing this separation we address the #10882 requirements. After a type check only methods available on the `methodDispatchTypes` can be invoked. However the value can still be cast to all the possible types.
2024-12-12 07:29:00 +00:00

1.2 KiB

layout title category tags order
developer-doc Defining Functions syntax
syntax
functions
10

Conversions

Conversions are special functions associated with a type, named from and taking single that argument. Following example:

type Complex
    Num re:Float im:Float

Complex.from (that:Float) = Complex.Num that 0

defines type Complex and a conversion from Float which uses the provided Float value as real part of a complex number while setting the imaginary part to zero.

Type Checks

Conversions are integral part of type checking during runtime. Having the right conversion in scope one can write:

complex_pi = 3.14:Complex

to create a new instance of type Complex. The Enso runtime represents the 3.14 literal as Float value and that would fail the Complex type check if there was no conversion method available. However as there is one, the runtime uses Complex.from behind the scene and complex_pi then becomes Complex.Num 3.14 0 value.

Type checks may perform no, one or multiple conversions (like in case of intersection types).