cobalt
cobalt copied to clipboard
Type inference for assignments
If a type is specified for an assignment then that type should be used when generating code. If a type isn't specified we should infer it. To do this we should have multiple patterns used to find out the type of the expression passed in. E.g.
let x = 10 # IntConst (Infer it is an IntType)
To do this we should write two functions. One for for inline and one for do blocks. With do blocks it will require a lot more so for now we should just focus on inline as it would mean just taking an expression and finding its type.
Do I understand correctly that this would be a part of some kind of preprocessing? I guess it will need to infer types of function that can be called on the righthand side of the assignment expressions. Currently the doc mentions inference for variable/value definitions and output types of functions. Will these be the only places where the programmer is allowed to omit the type?
Yes this will be added when converting the AST to the IR. If the type isn't specified we would infer the type for the assignment. For inline it shouldn't be too difficult although do blocks will be so we can leave it for the moment. I see no reason we can't allow this for any val/var and method definitions. Although there are some limitations with type inference.
This works for inline expressions. Block expressions will just get the type of the first expression for now.