-
Notifications
You must be signed in to change notification settings - Fork 3
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
Overriding traits #45
Comments
Scala's |
Alternatively we implement implicits (probably resolve based on names, not types, similar to Koka) and use implicits in these kind of use cases. Implicits may be useful in other contexts too. For example, in PL implementation you may want to pass around some kind of "context" type that you basically never change (only update its fields). It may be convenient to pass these around as implicits. |
It would be good if we could implement this without implicits to not add a feature that overlaps with an existing feature (traits) quite a bit in terms of use cases, and without new syntax. I wonder if we could expose the traits as constant records of functions and give the user to construct their own constant records of functions, to override some of the methods. For example, we have this
An implementation of this for
or
would be implemented as
Then we implement:
This would allow expressing something like: "call ToStr.toStr on this type, using this record for Another idea:
If we go down this route we should also allow named alternative
or maybe with inline syntax as well:
or perhaps
|
This requires quite a bit of refactoring in the current implementation before we can start experimenting. First, we currently implement runtime polymorphism by indexing a global dispatch table (conceptually, in the implementation we have a bunch of maps and vectors). This assumes single-parameter traits, which is fine for now. But it also indexes the dispatch table using the trait method's Since we can't change object header or the dispatch table in Tentative plan:
After this, |
One of the things traits don't make easy is overriding an instance used as a part of another instance.
This commonly comes up in traits like
Debug
orDisplay
(orShow
in Haskell) where we have complicated nested types (e.g. lists of sets of AST nodes), and we want to print the thing differently in different use sites.For example, in one call site I may want to abbreviate some AST nodes, in other I may want to print them in detail.
This can be done with implicits. Example in Koka:
But this has an issue: if an intermediate type (
rbset
) between the top-level type (list
) and the type we want to override a method of (int
) is not accessible (maybe it's a private type) then we can't call itsshow
method and can't override what it calls on its elements.The good part is, because it's not type directed, if the type has multiple
int
s (e.g. a pair oflist<int>
s), we can override some of them but not the others.I suspect if we had a way of saying "override ToStr instances for I32 in this type" that would probably be good enough.
The text was updated successfully, but these errors were encountered: