-
-
Notifications
You must be signed in to change notification settings - Fork 64
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
Extension methods #260
Comments
To clarify, my use case is a
|
This reminds me at least incidentally of Kotlin and C#'s extension methods, which have been great in their respective ecosystems for developing project- and domain- specific extensions to the standard library. In this vein I think it makes a great complement to Racket's language-oriented programming. |
Kotlin's extension methods were exactly my inspiration here 🙂 |
I see that Kotlin extension methods are static and resolved by scope, the same as Rhombus namespace extensions. Along those lines, it seems possible how to make static extension methods work — but it would mean that you can only call an extension method on an object with With private fields and methods, we have already ventured into into the space where some class members are accessible only via |
Unfortunately that means changing a method from a regular method to an extension method can break programs, especially programs that don't use annotations much. But I think that drawback could be acceptable, given how useful extension methods are for untangling cyclic dependencies and for creating DSLs. I think we should give it a shot. |
Oh, one more important point: extension methods should be available on subtypes too. So |
I hope it was not discussed before, but I have a question: I was recently looking at racket-lua looking for some low handling ideas for the optimization. It has "global" monkey patching, because the official version of lua has "global" monkey patching. My concern is that it's very unfriendly with the optimization, for example Is the idea here to add "global" monkey patching or "local" monkey patching? (I'm not sure about the correct technical term.) A "local" version can be optimizer friendly if it expands to a few macros under the hood (like functions with optional parameters in Racket). Also, what happens with diamond inheritance? |
I don't think the idea here would count as monkey-patching. It's scope-based, and not mutation-based. If you don't import the module that re-exports With extensible namespace as currently implemented, diamond importing works. If a namespace like |
I don't know how to make this work, after all. It does work to extend the
That approach does not make The problem with supporting extension methods is not just dynamic dispatch. Even if Longer explanation: The macro system can find bindings based on symbols plus scopes, but the needed lookup here depends on what the binding's compile-time value refers to (i.e., find an external-method binding that refers to the same other binding as an annotation). That mapping turns out to be difficult to set up outside the module system, in part because there's no way to get control over what happens when a name is imported by Since it seems that we're far from making extension methods work in Rhombus, and since extension methods are not clearly a complete solution for the original problem (no overriding and no dynamic dispatch), I think we should probably look for other solutions to the original problem. |
Hmm. Let's abandon this idea for now and keep it in mind as something to address in the future. For the collection APIs, I'm gonna just not bother adding a fluent method for copying sequences. Instead I'll add |
Currently, namespaces can be extended, and classes double as namespaces. But that's not enough to support extension methods. I'd like this to work:
The text was updated successfully, but these errors were encountered: