multimethod

why there are no multimethods in c++? [closed]

谁都会走 提交于 2019-12-05 01:14:55
I read many article about how to implement multimethod in c++: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1529.html http://www.codeproject.com/KB/recipes/mmcppfcs.aspx http://lambda-the-ultimate.org/node/2590 http://parasol.tamu.edu/people/peterp/omm/ why there are no multimethod in c++? why do not they get supported by c++ standard? Straight from the horse's mouth: "I rejected multi-methods with regret, because I liked the idea, but couldn't find an acceptable form under which to accept it." Bjarne Stroustrup, The Design and Evolution of C++ (p297) And later: "Multi-methods is

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

北慕城南 提交于 2019-12-05 01:12:39
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 ) { put "Str method" } multi method do-it ( Rat $r ) { put "Rat method" } } class Bar is Foo { multi method do-it { put "Bar method" } multi method do-it (*@a) { put "Bar slurpy method" } } Foo.new.do-it: 1; Foo.new.do-it: 'Perl 6'; Foo.new.do-it: <1/137>; Foo.new.do-it; put '-' x 10; Bar.new.do-it: 1; Bar.new.do-it: 'Perl 6'; Bar.new.do-it: <1/137>; Bar.new.do

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

℡╲_俬逩灬. 提交于 2019-12-04 17:00:23
问题 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 [m] (println "The colour is" (:colour m))) (defmulti which-colour-mm (fn [m f] (:colour m))) (defmethod which-colour-mm :blue [m f] (f m)) (defmethod which-colour-mm :red [m f] (f m)) (defmethod which-colour-mm :default [m f] (println "Default:

How to set and get multimethod metadata in clojure?

孤街浪徒 提交于 2019-12-01 01:39:42
I'm using multimethods to parse command line commands and their arguments. (defmulti run (fn [command args] command)) (defmethod run :default [& _] ...) ^{:args "[command]"} (defmethod run "help" [_ & [args]] "Display command list or help for a given command" ...) ^{:args ""} (defmethod run "version" [_ & [args]] "Print program's version" ...) (defn -main [& args] (run (first args) (next args))) When I try to access the metadata, for a specific method, clojure returns nil : (meta ((methods run) "help")) There's no such possibility. The first reason (straightforward one) is that defmethod doesn

.Net 4.0 Optimized code for refactoring existing “if” conditions and “is” operator

孤人 提交于 2019-11-30 18:09:17
问题 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 model, and the destination is purely an artifact of one particular application using that business model. CODE public class Role { } public class Manager : Role { } public class Accountant : Role { } public class Attender : Role { } public class

pretty-printing a record using a custom method in Clojure

自古美人都是妖i 提交于 2019-11-29 10:09:58
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 MyRecord [x ^java.io.Writer writer] (print-dup (:a x) writer)) (println (MyRecord. 'a 'b)) ;; a -- OK (clojure.pprint/pprint (MyRecord. 'a 'b)) ;; {:a a, :b b} -- not OK, I want a I would like clojure.pprint/pprint to also use my cutsom printer (which now, should just pretty-prints whatever is in the field a of the record for illustration purposes).

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

风格不统一 提交于 2019-11-28 22:56:14
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 Haskell has some really awesome solutions to the expression problem with the TypeClass . In particular - we can do: class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool member :: (Eq a) => a -> [a] -> Bool member y [] = False member y (x:xs) = (x == y) || member y xs Now in Clojure there are multimethods - so you can do: (defmulti area :Shape)

Clojure multimethods vs. protocols

天大地大妈咪最大 提交于 2019-11-28 04:39:10
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 language after multimethods, and that protocols generally have better performance so my question is this: Are protocols meant to replace multimethods? If not, could you give me an example where I would use multimethods instead of protocols? Arthur Ulfeldt Multimethods are more powerful and more expensive, use protocols when they are sufficient but if

pretty-printing a record using a custom method in Clojure

帅比萌擦擦* 提交于 2019-11-28 03:29:32
问题 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 MyRecord [x ^java.io.Writer writer] (print-dup (:a x) writer)) (println (MyRecord. 'a 'b)) ;; a -- OK (clojure.pprint/pprint (MyRecord. 'a 'b)) ;; {:a a, :b b} -- not OK, I want a I would like clojure.pprint/pprint to also use my cutsom printer (which

Clojure multimethods vs. protocols

限于喜欢 提交于 2019-11-26 18:06:50
问题 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 language after multimethods, and that protocols generally have better performance so my question is this: Are protocols meant to replace multimethods? If not, could you give me an example where I would use multimethods instead of protocols? 回答1: