albrow / fo

An experimental language which adds functional programming features to Go.
Other
1.24k stars 34 forks source link

Type argument inference #1

Open albrow opened 6 years ago

albrow commented 6 years ago

For now, Fo always requires type arguments and does not support type argument inference. I don't expect that the algorithm for inferring type arguments (at least most of the time) will be very complicated. We can always fallback to requiring type arguments if the type-checker can't infer them. This makes it possible to implement a simpler type argument inference algorithm at first and then improve it over time in a backwards-compatible way.

The tricky bit is that the part of the type-checker which converts an ast.TypeArgExpr into a concrete type is separate from the part that looks at, e.g., the function arguments or rhs of a composite literal that would be required for type inference.

A good place to start is the typeArgsRequired function. Right now it detects cases where a generic type is being used without type parameters and returns an error. Instead we should return a new data structure which represents a generic type that still needs type arguments. Later on, the type-checker will detect, e.g., a function call where the function type is this new data structure. It will then attempt to infer the type arguments based on the function arguments, and if it cannot, return an error similar to the one we return now.

chewxy commented 6 years ago

If you need help with type inference, I wrote a HM package: https://github.com/chewxy/hm. Happy to help