-
Notifications
You must be signed in to change notification settings - Fork 6
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
Support for multiple dispatch #6
Comments
That's an interesting idea that could be a nice generalization of virtual class methods. Although it doesn't seem to have many use-cases, a physics collision detection mechanism is a good one. I'll see how difficult it would be to implement. |
Actually it is very useful, especially when combined with UCFS, where you can separate data types from functions. For example: class Pet def
speaks: String = ""
class Cat(Pet) def
speaks: String = "Mew"
class Dog(Pet) def
speaks: String = "Huf"
func speak(pet: Animal): Void def
print pet.speaks
Cat().speak()
Dog().speak() This was only single dispatch, bet for example, if you want to add custom responses, depending on situation: class Mood def
class Happy(Mood) def
func speak(pet: Cat, mood: Mood): Void def
print "Mrrrr" Multiple dispatch is similar thing, to function overloading, but which function to call is decided at runtime. I find it very useful. I don't know how C++ is capable of dynamic type inference, but in general it is very difficult, unless it is already solved in C++. |
For Python I use multipledispatch. It does not support full type inference, for example |
Another good use case is binary operator overloading, for example: func add(a: Vector, b: Vector): Vector def
... |
Yeah, operator overloading is something I've been wondering about. It doesn't exactly fit as a class method, but more as a function outside of class definition, so it would probably work well with UFCS and multiple dispatch. This would be a big change to the language, but maybe worth implementing. Though I'm a bit sceptical about putting all methods into the global namespace. C++ does only have single dispatch. There are ways of simulating multiple dispatch, like here: https://eli.thegreenplace.net/2016/a-polyglots-guide-to-multiple-dispatch/, but it's not very readable and not scalable enough. Probably some map (dictionary) of functions, with tuples of argument types as keys, would be necessary for a general implementation. I think that's how Python's |
All methods definitely should not be put into the global namespace. In order for a method to be considered, that method should be explicitly imported into module or be defined in that module. For example if you want to add two use vectors
a = Vector([1, 2])
b = Vector([3, 4])
print a + b |
Do you have plans to add support for multiple dispatch, like in Julia language?
The text was updated successfully, but these errors were encountered: