Questions tagged [multimethod]

90 questions
89
votes
1 answer

What is "polymorphism a la carte" and how can I benefit from it?

In his talk Simple Made Easy, Rick Hickey talks about "Polymorphism a la carte" (about 30:00 into the video). In the same context, he also mentions Haskell's Type Classes and Clojure's Multi-Methods (and protocols). Since I am not very familiar with…
Grega Kešpret
  • 11,827
  • 6
  • 39
  • 44
45
votes
4 answers

Clojure multimethods vs. protocols

I'm a Clojure novice and was looking for some concrete examples of when to use protocols and when to use multimethods. I know that protocols are generally geared towards creating a type hierarchy and typical OOP things, that they were added to the…
Sean Nilan
  • 1,745
  • 1
  • 14
  • 21
32
votes
7 answers

How do multimethods solve the namespace issue?

I am researching programming language design, and I am interested in the question of how to replace the popular single-dispatch message-passing OO paradigm with the multimethods generic-function paradigm. For the most part, it seems very…
Patrick Li
  • 672
  • 4
  • 11
26
votes
2 answers

What are the reasons that protocols and multimethods in Clojure are less powerful for polymorphism than typeclasses in Haskell?

More broadly this question is about various approaches to the expression problem. The idea is that your program is a combination of a datatype and operations over it. We want to be able to add new cases without recompiling the old classes. Now…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
20
votes
3 answers

Is it possible to overload Clojure multi-methods on arity?

I have some code that uses multi-methods and would ideally like to overload the function (in this case, multi-function) so that I can pass in a higher order function to help with testing, for example. Here's the example: (ns multi) (defn my-print…
Andrew Whitehouse
  • 2,738
  • 3
  • 31
  • 35
19
votes
3 answers

Why are Clojure's multimethods better than 'if' or 'case' statements

I've spent some time, trying to understand Clojure multimethods. The main "pro" multimethod argument, as far as I understand, is their flexibility, however, I'm confused with the argumentation of why multimethods are better than a simple if or case…
Iger
  • 416
  • 2
  • 9
17
votes
1 answer

How does Julia implement multimethods?

I've been reading a bit from http://c2.com/cgi/wiki?ImplementingMultipleDispatch I've been having some trouble finding reference on how Julia implements multimethods. What's the runtime complexity of dispatch, and how does it achieve it?
math4tots
  • 8,540
  • 14
  • 58
  • 95
13
votes
3 answers

why there are no multimethods in c++?

I read many article about how to implement multimethod in…
12
votes
1 answer

Scheme -> Clojure: multimethods with predicates in the methods?

I'm converting some Scheme code to Clojure. The original uses a dispatching pattern that's very similar to multimethods, but with an inverted approach to the matching predicates. For example, there a generic function "assign-operations". The…
user73774
12
votes
2 answers

Is there any difference between multimethod and multipledispatch?

I would like to use overloading in Python. I know it's not possible by design (Python is dynamically typed language), there is quite good thread here on this topic. That's why I could use something like multiple dispatch (multimethods). And I'm…
Nerxis
  • 3,452
  • 2
  • 23
  • 39
12
votes
2 answers

How does a Perl 6 object find a multi method that might be in a parent class or role?

Consider this example where a subclass has a multi method with no signature and one with a slurpy parameter: class Foo { multi method do-it { put "Default" } multi method do-it ( Int $n ) { put "Int method" } multi method do-it ( Str $s…
brian d foy
  • 129,424
  • 31
  • 207
  • 592
12
votes
2 answers

Using Clojure multimethods defined across multiple namespaces

Although the below example seems a bit strange, it's because I'm trying to reduce a fairly large problem I've got at present to a minimal example. I'm struggling to work out how to call into multimethods when they're sitting behind a couple of…
monch1962
  • 5,151
  • 5
  • 31
  • 38
11
votes
2 answers

What does retag parameter in s/multi-spec mean?

Can you explain with examples how does retag parameter impacts multi-spec creation? I find multi-spec documentation hard to digest.
OlegTheCat
  • 4,443
  • 16
  • 24
11
votes
3 answers

How would you idiomatically extend arithmetric functions for other datatypes in Clojure?

So I want to use java.awt.Color for something, and I'd like to be able to write code like this: (use 'java.awt.Color) (= Color/BLUE (- Color/WHITE Color/RED Color/GREEN)) Looking at the core implementation of -, it talks specifically about…
SCdF
  • 57,260
  • 24
  • 77
  • 113
11
votes
3 answers

pretty-printing a record using a custom method in Clojure

In Clojure 1.5.0, how can I provide a custom pretty-printer for my own record type, defined with defrecord. (defrecord MyRecord [a b]) (defmethod print-method MyRecord [x ^java.io.Writer writer] (print-method (:a x) writer)) (defmethod print-dup…
namin
  • 37,139
  • 8
  • 58
  • 74
1
2 3 4 5 6