Multimethods

50 %
50 %
Information about Multimethods
Technology

Published on August 9, 2010

Author: amanking

Source: slideshare.net

Description

This is a lightning talk I gave at an internal event called Techquilla in ThoughtWorks Pune office.

More details: http://www.wikyblog.com/AmanKing/Multimethods

Multimethodsan introduction Aman King king@thoughtworks.com

What is it?

Multiple dispatch

Basically: a language feature

Before the details…

Some Java code snippets…

public class MultimethodsDemo { static class Walk { } static class Dance { } static class Attack { } static class Robot { public String doThis(Walk walk) { return "I am walking..."; } public String doThis(Dance dance) { return "Let's boogie woogie..."; } public String doThis(Attack attack) { throw new RuntimeException("I don't believe in violence!"); } } public static void main(String[] args) { Robot robot = new Robot();System.out.println( robot.doThis( new Walk() ));System.out.println( robot.doThis( new Dance() ));System.out.println( robot.doThis( new Attack() )); }}

I am walking...Let's boogie woogie...Exception in thread "main" java.lang.RuntimeException: I don't believe in violence! at MultimethodsDemo$Robot.doThis(MultimethodsDemo.java:10) at MultimethodsDemo.main(MultimethodsDemo.java:18)

Polymorphism

Overloading

public class MultimethodsDemo { static class Walk { } static class Dance { } static class Attack { } static class Robot { public String doThis(Walk walk) { return "I am walking..."; } public String doThis(Dance dance) { return "Let's boogie woogie..."; } public String doThis(Attack attack) { throw new RuntimeException("I don't believe in violence!"); } } public static void main(String[] args) { Robot robot = new Robot();System.out.println( robot.doThis( new Walk() ));System.out.println( robot.doThis( new Dance() ));System.out.println( robot.doThis( new Attack() )); }}

import java.util.*;public class MultimethodsDemo {static interface Command {} static class Walk implements Command { } static class Dance implements Command { } static class Attack implements Command { } static class Robot { public String doThis(Walk walk) { return "I am walking..."; } public String doThis(Dance dance) { return "Let's boogie woogie..."; } public String doThis(Attack attack) { throw new RuntimeException("I don't believe in violence!"); } } public static void main(String[] args) { Robot robot = new Robot(); List<Command> commands = Arrays.asList( new Walk(), new Dance(), new Attack() );for ( Command command : commands ) {System.out.println( robot.doThis(command) ); } }}

MultimethodsDemo.java:20: cannot find symbolsymbol : method doThis(MultimethodsDemo.Command)location: class MultimethodsDemo.RobotSystem.out.println( robot.doThis(command) ); ^1 error

Solution?

Satisfy the compiler!

import java.util.*;public class MultimethodsDemo { static interface Command {} static class Walk implements Command {} static class Dance implements Command {} static class Attack implements Command {} static class Robot { public String doThis(Walk walk) { return "I am walking..."; } public String doThis(Dance dance) { return "Let's boogie woogie..."; } public String doThis(Attack attack) { throw new RuntimeException("I don't believe in violence!"); }public String doThis(Command command) { throw new RuntimeException("Unknown command: " + command.getClass()); } } public static void main(String[] args) { Robot robot = new Robot(); List<Command> commands = Arrays.asList( new Walk(), new Dance(), new Attack() ); for ( Command command : commands ) {System.out.println( robot.doThis(command) ); } }}

Made the compiler happy but…

Exception in thread "main" java.lang.RuntimeException: Unknown command: class MultimethodsDemo$Walk at MultimethodsDemo$Robot.doThis(MultimethodsDemo.java:15) at MultimethodsDemo.main(MultimethodsDemo.java:23)

Early Binding(compile-time)

Now what?

Forcing correct overloadingatcompile-time…

// … static class Robot { public String doThis(Walk walk) { return "I am walking..."; } public String doThis(Dance dance) { return "Let's boogie woogie..."; } public String doThis(Attack attack) { throw new RuntimeException("I don't believe in violence!"); } public String doThis(Command command) { if (command instanceof Walk) { return doThis( (Walk) command ); } if (command instanceof Dance) { return doThis( (Dance) command ); } if (command instanceof Attack) { return doThis( (Attack) command ); } throw new RuntimeException("Unknown command: " +command.getClass()); } } // ...

or use Visitor pattern.

So what went wrong?

Java does early binding for overloading polymorphism

But remember…

Java does late binding (runtime)for overriding polymorphism

public class MultimethodsDemo { static class Robot { public String name() { return "normal robot"; } } static class AwesomeRobot extends Robot { public String name() { return "awesome robot!!!"; } } public static void main(String[] args) {Robot robot = new Robot();Robot awesomeRobot = new AwesomeRobot();System.out.println( robot.name() ); // normal robotSystem.out.println( awesomeRobot.name() ); // awesome robot!!! }}

Now think ofrobot.doThis(command)asdoThis(robot, command)

C# 3.0 : Extension Methodsnamespace CustomExtensions{ public static class CustomExtensionsClass { public static string Times(thisobject input, int times) { return "don't care!"; } public static string Times(thisstring input, int times) { string result = "";for(inti = 0; i < times; i++) result += input; return result; } public static intTimes(thisint input, int times) { return input * times; } }}// …using CustomExtensions;varaStringObject = "aman"; aStringObject.Times(2); // "amanaman"varanIntObject = 3; anIntObject.Times(2); // 6// …

In these examples, polymorphism is based on the runtime type of a single argument: the 1st one (implicitly passed)

In other words…

Java and C# 3.0support Single Dispatch

not Double Dispatch,not Multiple Dispatch.

So what is Multiple Dispatch?

Selection aka dispatchof a method based on the runtime type of multiple arguments

Languages that support Multiple Dispatch / Multimethods: Common LispClojure Perl GroovyC# 4.0

Common Lisp

Clojure

Perl

Groovy

C# 4.0

Add a comment

Related presentations

Related pages

Multiple dispatch - Wikipedia, the free encyclopedia

Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run ...
Read more

Clojure - Multimethods and Hierarchies

The multimethod system exposes this API: defmulti creates new multimethods, defmethod creates and installs a new method of multimethod associated with a ...
Read more

Multi Methods - Cunningham & Cunningham, Inc.

MultiMethods are sets of methods that are polymorphic on more than one of their arguments. Implemented in languages such as CommonLispObjectSystem, ...
Read more

multimethods 1.0.0 : Python Package Index

multimethods 1.0.0. Download multimethods-1.0.0.tar.gz. A simple python multidispatch. A multimethod implementation, loosely based on Guido’s initial ...
Read more

Multimethods and Hierarchies | springerprofessional.de

Clojure is not an object-oriented language in the traditional sense of classes and methods, although it is built on Java’s object-oriented
Read more

Structured Clojure: Protocols and multimethods (oh my ...

Structured Clojure: Protocols and multimethods (oh my!) This blog is sponsored by Worklog Assistant, a JIRA time tracker. Start your free trial now!
Read more

Visitor Pattern Versus Multimethods - The Nice programming ...

Visitor Pattern Versus Multimethods. The Visitor Pattern The visitor pattern is a programming pattern that has been advocated strongly for writing code ...
Read more

Multimethod.js - A Clojure-inspired multimethod library ...

Inspired by Clojure's multimethods, multimethod.js provides a functional alternative to classical, prototype based polymorphism.
Read more

MultiMethods in C++: Finding a complete solution - CodeProject

Download sample files - 23.8 Kb; Preamble. This paper provides a way to solve famous "multimethods problem". The main merits of the proposed solution are:
Read more

Multimethods — Dylan Programming

Multimethods¶ In this chapter, we show two important techniques. First, we define methods for built-in generic functions — in this case, for the ...
Read more