Michael2109 / cobalt

The Cobalt programming language
GNU Lesser General Public License v3.0
37 stars 10 forks source link

Type inference for assignments #506

Open Michael2109 opened 6 years ago

Michael2109 commented 6 years ago

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 commented 6 years ago

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.

FilipJanitor commented 6 years ago

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?

Michael2109 commented 6 years ago

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 commented 6 years ago

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