Added note about type unions in generic types

This commit is contained in:
Mattia Giambirtone 2022-12-15 15:29:18 +01:00
parent f0c4d1c152
commit d3220de5bf
1 changed files with 22 additions and 0 deletions

View File

@ -170,6 +170,28 @@ When using automatic types, peon will behave similarly to C++ (think: templates)
typecheck and compile the function once it is called with a given type signature. For this reason,
automatic and parametrically polymorphic types cannot be used together in peon code.
Another noteworthy concept to keep in mind is that of type unions. For example, take this snippet:
```
fn foo(x: int32): int32 {
return x;
}
fn foo(x: int): int {
return x;
}
fn identity[T: int | int32](x: T): T {
return foo(x);
}
```
This code will, again, fail to compile: this is because as far as peon is concerned, `foo` is not
defined for both `int` and `int32` _at the same time_. In order for that to work, `foo` would need
to be rewritten with `T: int32 | int` as its generic argument type in order to avoid the ambiguity.
#### More generics