Questions tagged [multimethod]
90 questions
10
votes
2 answers
Perl 6 multi methods never match expected signature
I have a class with two multi methods (multi submit).
I call my multi like this:
$perspective.submit(:message($message.content));
Which gets shipped off to my class:
my $perspective-api =…

kawaii
- 301
- 2
- 8
9
votes
2 answers
General syntax of multimethods
I apologize if the question is trivial, but some googling is not leading me anywhere. What is the general syntax of defmulti and defmethod? I can write simple multimethods, but I am not sure where I can put the docstring, pre and post conditions,…

Andrea
- 20,253
- 23
- 114
- 183
9
votes
1 answer
Clojure - dispatch on return type? (As expressive as Haskell Typeclasses)
This is a question about the expressiveness of Clojure vs other languages such as Haskell. The broader issue is solutions to the Expression Problem
This question reached the conclusion that in general Clojure protocols (and multimethods) were less…

hawkeye
- 34,745
- 30
- 150
- 304
8
votes
3 answers
defmethod catch all
I have a multimethod that specializes on two parameters:
(defmulti get-tag-type (fn [type tag] [type tag]))
Having the type allows me to group the different defmethod calls into sets:
(defmethod get-tag-type [::cat 0] [type tag] ::tiger)
(defmethod…

Brigham
- 14,395
- 3
- 38
- 48
8
votes
2 answers
When should Haskell functions take tuples, rather than multiple arguments?
In http://www.haskell.org/pipermail/haskell-cafe/2007-August/030096.html the typeclass method collide is defined as taking a 2-tuple as its single argument, rather than two "normal" arguments (I think I understand partial application, etc.).
{-#…

fadedbee
- 42,671
- 44
- 178
- 308
8
votes
1 answer
How to dispatch multimethod on primitive types?
I want my program act differently between primitive types and their wrapper classes, for example:
(defmulti try-type class)
(defmethod try-type Integer [arg]
(println "Integer"))
(defmethod try-type Integer/TYPE [arg]
(println "int"))
But it…

qiuxiafei
- 5,827
- 5
- 30
- 43
7
votes
3 answers
Are clojure multimethods slow by nature
I was looking at the clojure.core function re-groups:
(defn re-groups [^java.util.regex.Matcher m]
(let [gc (. m (groupCount))]
(if (zero? gc)
(. m (group))
(loop [ret [] c 0]
(if (<= c gc)
(recur…

M Smith
- 1,988
- 15
- 28
7
votes
2 answers
Did the Loki multimethods make it into C++11?
I am reading Modern C++ Design Generic Programming and Design Patterns Applied by Andrei Alexandrescu and chapter 11 on multimethods deals exactly with the problem I am trying to solve. All source code from the book is published in a library named…

Martin Drozdik
- 12,742
- 22
- 81
- 146
6
votes
1 answer
Looping through args of macro
I am trying to write a macro in Clojure that allows for evaluation of a series of simple "def" expressions. I am a n00b when it comes to macros. The idea is that
(my-defs y1 1
y2 "taco")
should expand to
(do (def y1 1) (def y2 "taco"))
The…

Gabriel Mitchell
- 979
- 5
- 17
6
votes
1 answer
Can I use Clojure's derive to create a hierarchy of my defrecord class types?
I would like to do something like:
(defrecord Base [])
(defrecord Person [])
(defrecord Animal [])
(derive Person Base)
(derive Animal Base)
(isa? Animal Person)
Is this possible?
Update:
I've since realized that this is not possible so I am…

yazz.com
- 57,320
- 66
- 234
- 385
6
votes
1 answer
Implementing a multimethod in separate files in different namespace
I am trying to define a multimethod and its implementation in a separate file. It goes something like this:
In file 1
(ns thing.a.b)
(defn dispatch-fn [x] x)
(defmulti foo dispatch-fn)
In file 2
(ns thing.a.b.c
(:require [thing.a.b :refer…

Abhiroop Sarkar
- 2,251
- 1
- 26
- 45
6
votes
3 answers
Colliding stellar objects via multimethods in OO Prolog?
I wonder how one would combine unification and OO in Prolog. I would like to implement a multimethod dispatch on term objects.
Without term objects and simple terms I would do the following and could profit from multi-argument…
user502187
5
votes
3 answers
Why does a Perl 6 Str do the Positional role, and how can I change []?
I'm playing around with a positional interface for strings. I'm aware of How can I slice a string like Python does in Perl 6?, but I was curious if I could make this thing work just for giggles.
I came up with this example. Reading positions is…

brian d foy
- 129,424
- 31
- 207
- 592
5
votes
3 answers
Defining Clojure multimethods
I have the following in one namespace say shapes:
(derive ::rect ::shape)
(derive ::square ::rect)
Now executing the following in the shapes namespace:
(isa? ::square ::shape)
returns true. But when I execute the following in a namespace where I…

Hamza Yerlikaya
- 49,047
- 44
- 147
- 241
5
votes
1 answer
core.async go block fails to compile when protocol invocation form contains
I was implementing a function involving a core.async go block, when I stumbled on a strange compilation error :
CompilerException java.lang.IllegalArgumentException:
No method in multimethod '-item-to-ssa' for dispatch value: :protocol-invoke,…

Valentin Waeselynck
- 5,950
- 26
- 43