Multimethods

0 views

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
0
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Multimethods

  1. 1. Multimethodsan introduction<br />Aman King<br />king@thoughtworks.com<br />
  2. 2. What is it?<br />
  3. 3. Multiple dispatch<br />
  4. 4. Basically: a language feature<br />
  5. 5. Before the details…<br />
  6. 6. Some Java code snippets…<br />
  7. 7. 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() )); }}<br />
  8. 8. 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)<br />
  9. 9. Polymorphism<br />
  10. 10. Overloading<br />
  11. 11. 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() )); }}<br />
  12. 12. 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) ); } }}<br />
  13. 13. MultimethodsDemo.java:20: cannot find symbolsymbol : method doThis(MultimethodsDemo.Command)location: class MultimethodsDemo.RobotSystem.out.println( robot.doThis(command) ); ^1 error<br />
  14. 14. Solution?<br />
  15. 15. Satisfy the compiler!<br />
  16. 16. 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) ); } }}<br />
  17. 17. Made the compiler happy but…<br />
  18. 18. 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)<br />
  19. 19. Early Binding(compile-time)<br />
  20. 20. Now what?<br />
  21. 21. Forcing correct overloadingatcompile-time…<br />
  22. 22. // … 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()); } } // ...<br />
  23. 23. or use Visitor pattern.<br />
  24. 24. So what went wrong?<br />
  25. 25. Java does early binding for overloading polymorphism<br />
  26. 26. But remember…<br />
  27. 27. Java does late binding (runtime)for overriding polymorphism<br />
  28. 28. 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!!! }}<br />
  29. 29. Now think ofrobot.doThis(command)asdoThis(robot, command)<br />
  30. 30. 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// …<br />
  31. 31. In these examples, polymorphism is based on the runtime type of a single argument: the 1st one (implicitly passed)<br />
  32. 32. In other words…<br />
  33. 33. Java and C# 3.0support Single Dispatch<br />
  34. 34. not Double Dispatch,not Multiple Dispatch.<br />
  35. 35. So what is Multiple Dispatch?<br />
  36. 36. Selection aka dispatchof a method based on the runtime type of multiple arguments<br />
  37. 37. Languages that support Multiple Dispatch / Multimethods:<br /><ul><li> Common Lisp
  38. 38. Clojure
  39. 39. Perl
  40. 40. Groovy
  41. 41. C# 4.0</li></li></ul><li>C# 4.0 : Multimethods using ‘dynamic’using System;namespace Multimethods{ public interface ICommand { } public class Walk : ICommand { } public class Dance : ICommand { } public class Attack : ICommand { } public 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 InvalidOperationException("I don't believe in violence!"); } public string DoThis(ICommand command) { throw new InvalidOperationException("Unknown command: " + ((object) command).GetType().Name); } public string DoThisCommand(ICommand command) {dynamiccmd = command; return DoThis(cmd); } }}<br />
  42. 42. // …Robot robot = new Robot();ICommandcommand = newWalk();robot.DoThisCommand( command ); // "I am walking... "robot.DoThis( command ); // Exception: "Unknown command:Walk"// …<br />
  43. 43. So what is interesting about Multiple Dispatch /Multimethods?<br />
  44. 44. For starters…<br />
  45. 45. Thinking about…<br />
  46. 46. OO as syntactic sugarfor method dispatch?<br />
  47. 47. Design patterns as a way to overcome lack of language features?Visitor for Double DispatchStrategy for Closures/Lambdas<br />
  48. 48. Do we really need more than Single Dispatch or at best Double Dispatch?<br />
  49. 49. References<br /><ul><li>http://en.wikipedia.org/wiki/Multiple_dispatch
  50. 50. http://www.c2.com/cgi/wiki?MultiMethods
  51. 51. http://en.wikipedia.org/wiki/Extension_method
  52. 52. http://clojure.org/multimethods</li></li></ul><li>Thank you<br />http://www.wikyblog.com/AmanKing<br />

×