Morpho-lang / morpho

The Morpho language 🦋. Morpho is a small embeddable language for scientific computing applications.
MIT License
30 stars 10 forks source link

Multiple Dispatch #258

Closed softmattertheory closed 3 months ago

softmattertheory commented 4 months ago

This PR adds initial support for typing and multiple dispatch to Morpho. You can define multiple implementations of the same function:

fn f(List x) { .. }
fn f(String x) { .. }

and morpho will dispatch to the correct implementation based on the type at runtime. Multiple dispatch resolutions are compiled to a special implementation (a directed acyclic graph) for efficient execution, imposing typically only a 20% overhead on a regular function call per argument. Future compiler optimizations will reduce the need for this.

Multiple dispatch can similarly be used for method definitions, and are resolved at runtime. PR includes extensive tests and online help.

Typed variables and support for type tracking in the compiler are implemented, but type checking is not fully implemented yet. (This PR is a necessary step towards full type checking).

joshichaitanya3 commented 3 months ago

This looks great! This would both simplify and improve so many core things! I love the improvement on the List_sort API. And we got the visitor pattern in graphics already working with multiple dispatch. Great step for both users and developers!

I have added some comments with small suggested changes / additions.

One thing I would recommend is adding something like a types.md help file where it would list all the in-built types and their details, future supported in-built types (Callable?), and syntax, etc. Since this is a big change and more related features are a WIP, it would be great to have this documented as it would give users an idea of what currently works and what would work in the future. This is not urgent, but would be really helpful if it exists when this feature is released.

Really excited for this!