mirth-lang / mirth

Compiler for the Mirth programming language.
BSD Zero Clause License
447 stars 14 forks source link

New def syntax and type inference. #301

Closed typeswitch-dev closed 6 months ago

typeswitch-dev commented 6 months ago

This PR adds a new def syntax:

def word [ type-sig ] { body }

which is sugar for def(word, type-sig, body).

In addition, the [type-sig] is optional, in which case a bit of type inference is implemented.

Because mirth is contextual, the type inference makes a few useful assumptions based on the name:

These asumptions help us avoid the need to write explicit type annotations in many situations, because having a little bit of type information (e.g. when defining a method) helps a lot with name resolution.

Ultimately the goal here isn't to get the most general type, it's to get the type the user wants most of the time. If the user wants something else, they can always write a type signature.

Also there is some support for recursive definitions. But it's always going to be much harder to infer those, especially because of stack polymorphism. Again, one can always write a type signature.