Questions tagged [multimethod]
90 questions
5
votes
3 answers
How can I force dispatch to an existing multimethod implementation?
If I declare a multimethod for another namespace (a library which I cannot change), ns-a, for my type:
defmethod ns-a/method-a Y [y]
and there's an existing method for X defined in ns-a:
defmethod method-a X [x]
and the dispatch function…

Hendekagon
- 4,565
- 2
- 28
- 43
4
votes
0 answers
Is it possible to use functools.singledispatch with composite/nested/container types in Python?
Edit 2022-08-30: perhaps with the introduction of variadic generics (PEP-646) in Python 3.11, dispatch with composite types may become possible.
I wonder whether the following can be achieved,
and if so, without requiring much extra code:
from…

jrbergen
- 660
- 5
- 16
4
votes
0 answers
Multimethod union types don't support overloading correctly
I'm using the multimethod package (1.4) to overload methods. I need to have Unioned types (or TypeVar) as the input types; however, this causes an issue. Example:
from typing import Union
from multimethod import multimethod
@multimethod
def…

Jacob Steinebronn
- 833
- 3
- 12
4
votes
3 answers
Nim - Create sequence of objects which implement a method
I want to program a game and would like to use a component pattern for multiple entities.
In a language with interfaces / type-classes / multiple inheritance there would be no problem.
I want some entities to be updateable but not renderable and…

Karroffel
- 115
- 6
4
votes
2 answers
Combining methods in Clojure
Suppose we have a multimethod foo. It has several realizations. Let's say that one of them is called when argument of foo is a string that contains character \r and another is executed when argument of foo is a string containing character \!.…

Mark Karpov
- 7,499
- 2
- 27
- 62
4
votes
1 answer
flask-restful having a get/ and post with json in the same class
The get method on user works if the # api.add_resource(User, '/user/')
line is uncommented, and the other api.add_resource is.
The inverse of that is true to make the post method work.
How can I get both of these paths to work?
from flask import…

tylerj
- 53
- 1
- 7
4
votes
2 answers
loading a file of multimethods only
scenario: i want to use multimethods to dispatch and spread functionality across multiple files. one of the files contains only multimethods, and to make them usable i must manually load the file. is there a way to automatically load the file,…

xen
- 133
- 6
4
votes
2 answers
Clojure: defmulti on different class types
Quick clojure question, I think this is mostly syntax related. How do I dispatch a multimethod based on specific type signatures of the arguments, for example:
(defn foo
([String a String b] (println a b))
([Long a Long b] (println (+ a…

David Williams
- 8,388
- 23
- 83
- 171
3
votes
1 answer
Reloading multimethods via Slime
I'm having trouble reloading multimethods when developing in Emacs with a Slime repl.
Redefining the defmethod forms works fine, but if I change the dispatch function I don't seem to be able to reload the defmulti form. I think I specifically added…

liwp
- 6,746
- 1
- 27
- 39
3
votes
0 answers
TypeError: type 'range' is not an acceptable base type
Could you please tell me what is wrong with this code?
from multimethod import multimethod
from typing import Iterable
@multimethod
def foo(x: Iterable[Iterable]):
print(x)
@multimethod
def foo(x):
print(x)
foo([range(1), range(2)])
The…

lambdakappatheta
- 145
- 1
- 5
3
votes
1 answer
Can I dispatch a mutli-method on both Type AND properties in Clojure?
I have a method called "visualize" in my Clojure application which can supposedly render any part of my application. The problem I have is that some things in my application are Java classes and some are hashmaps, with fields internally marking the…

yazz.com
- 57,320
- 66
- 234
- 385
3
votes
4 answers
Why Elixir doesn't use Multimethods / Protocols?
Let's see the example from docs:
square = fn(x) -> x * x end
list = [1, 2, 3, 4]
Enum.map(list, square)
Why does it requires to explicitly write Enum.map? Why it doesn't use clean and short notation map [1, 2, 3, 4], square?
The Elixir has…

Alex Craft
- 13,598
- 11
- 69
- 133
3
votes
1 answer
CLojure: Higher order functions vs protocols vs multimethods
there are plenty protocols vs multimethods comparisions, but why not to use higher order functions?
Let's come with example:
We have some data (record for example). And we have methods serialize and deserialize.
Say that we want to save it into…

ravenwing
- 668
- 3
- 20
3
votes
1 answer
Understanding multimethods dispatching
I can understand how this works:
(defmulti area :Shape)
(defmethod area :B [x] (println "Rect"))
(defmethod area :C [x] (println "Circle"))
(defmethod area :default [x] (do (println "Unknown") (:Shape x)))
(area {:Shape :B})
=> Rect
nil
(area…

m0skit0
- 25,268
- 11
- 79
- 127
3
votes
7 answers
.Net 4.0 Optimized code for refactoring existing "if" conditions and "is" operator
I have following C# code. It works fine; but the GetDestination() method is cluttered with multiple if conditions by using is operator.
In .Net 4.0 (or greater) what is the best way to avoid these “if” conditions?
EDIT: Role is part of the business…

LCJ
- 22,196
- 67
- 260
- 418