|
|
|
|
|
by akeefer
5274 days ago
|
|
In Gosu, it's pretty much the same (though enhancements are statically dispatched and thus subject to a different set of limitations): enhancement MyEnhancement<T> : T[] {
function filterMap() { . . . }
} Roughly the same in C# with extension methods, though with C# you explicitly import the extension method while in Gosu they're automatically always there (sort of good, sort of bad). Again, not exactly the same as the dynamic language examples, but enhancements/extension methods do allow you to extend existing classes in a reasonable fashion while still being amenable to all the other advantages static typing gives you. |
|
The more complex method he is proposing (filterMap) cannot be done at all in the languages you mention, though he only uses it precisely to get the most complex kind of method that Scala collections offer. But it is also possible, and I just blogged about it here: http://dcsobral.blogspot.com/2012/01/adding-methods-to-scala....
But, no, that is not what he wants. He wants to add this method not to the collections, but to something that isn't a collection. Well, Scala can do that too -- it added all the collections methods to String and Array, didn't it?
And here comes the twist: he wants to add filterMap not by adding it directly to them, like Scala does. He wants, instead, to go _through_ that code to get at them.
With extension methods, the login would be like this:
* X adds extension methods to Y * Z adds extension methods to X * Therefore, Z extension methods should be available on Y
And, in fact, it is even possible to do that in Scala for many methods, but not for the particular combination he chose, and while still inferring all types.