3 ms·
This only shows that it can do method type specialization to get rid of type dispatch. Can you add a new multimethod clause to an existing if-then-else type di
by compressedgas 6y ago
This only shows that it can do method type specialization to get rid of type dispatch.
Can you add a new multimethod clause to an existing if-then-else type dispatch method? For example add:
foo(x::Baz) = x.foo
Or is harm still done because the method is not open for extension when written like that?
- pdeffebach 6y agoYou can't edit a function like that. If you add a new function definitoin for foo(x::Baz) then you will never enter the method that has those if else statements. This is the benefit of multiple dispatch. You can't add more if else statements inside a method that is already written but you can add new methods.
- stabbles 6y agoManual dispatch can be an antipattern hindering composability though. If package A does: function foo(x) # some lines of code y = x isa Int ? 2.0 : 3x # some other lines that do things with y end Then package B would have to define foo(x::MyType), copy the contents from package A, change the `y = ...` bit, and make sure the rest stays in sync with package A. If instead package A did this: foo_magic(x::Int) = 2.0 foo_magic(x) = 3x function foo(x) # some lines of code y = foo_stuff(x) # some other lines that do things with y end Your package B could just add an extra definition of foo_magic(x::MyType) instead.