Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Dispatch #258

Merged
merged 118 commits into from
Jun 1, 2024
Merged

Multiple Dispatch #258

merged 118 commits into from
Jun 1, 2024

Conversation

softmattertheory
Copy link
Contributor

@softmattertheory softmattertheory commented May 20, 2024

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
Copy link
Collaborator

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!

help/functions.md Show resolved Hide resolved
help/functions.md Show resolved Hide resolved
test/types/multiple_dispatch/duplicate_no_type.morpho Outdated Show resolved Hide resolved
test/types/multiple_dispatch/namespace_for_new.morpho Outdated Show resolved Hide resolved
@softmattertheory softmattertheory merged commit d5485a2 into dev Jun 1, 2024
4 checks passed
@softmattertheory softmattertheory deleted the type branch June 1, 2024 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants