I have a toy hobby compiler I want to add some kind of generic functionality to, to be able to express things like “output should be same as input”. Case in point, an array_slice
function:
/**
* @template A
* @param array<A> $input
* @param int $index
* @return array<A>
*/
array_slice(array $input, int $index): array
Currently, the types in the AST look like:
type typ =
| Int
| Float
| Dynamic_array of typ
So I guess a natural extension would be something like:
type typ =
| Type_variable of string
| Int
| Float
| Dynamic_array of typ
And then when inferring the content of the type variable, just check for the string name, “A”? No fancy unification algorithm is needed, I think.
Any comments or ideas?