cobalt icon indicating copy to clipboard operation
cobalt copied to clipboard

Type inference for assignments

Open Michael2109 opened this issue 6 years ago • 4 comments

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)

Michael2109 avatar May 28 '18 13:05 Michael2109

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.

Michael2109 avatar Jun 05 '18 11:06 Michael2109

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?

FilipJanitor avatar Jun 05 '18 13:06 FilipJanitor

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.

Michael2109 avatar Jun 05 '18 19:06 Michael2109

This works for inline expressions. Block expressions will just get the type of the first expression for now.

Michael2109 avatar Jun 28 '18 15:06 Michael2109