< Return to Video

dotJS 2012 - Axel Rauschmayer - Multiple dispatch

  • 0:10 - 0:13
    Hello, so my talk is going to be on multiple dispatch.
  • 0:13 - 0:17
    First for starters what is method dispatch?
  • 0:17 - 0:19
    If you have a polymorphic method
  • 0:19 - 0:22
    that mean if you have multiple implementations
  • 0:22 - 0:26
    and we'll soon see an example to make it clear what I mean
  • 0:26 - 0:32
    if you have those multiple implementations, the dispatch determines
  • 0:32 - 0:35
    which of those implementations will be called when you
  • 0:35 - 0:38
    invoke the method.
  • 0:38 - 0:42
    JavaScript has simple dispatch, so a single value is used for dispatch
  • 0:42 - 0:47
    we will then look at something new
  • 0:47 - 0:50
    and that is called multiple dispatch.
  • 0:50 - 0:55
    You can either dispatch via an object or
  • 0:55 - 0:59
    via a function. And if you dispatch via an
  • 0:59 - 1:01
    object this is what it looks like.
  • 1:01 - 1:04
    You have a so-called polymorphic method
  • 1:04 - 1:11
    and the type of jane determines which of the two functions
  • 1:11 - 1:15
    will be called if you make that method call up here.
  • 1:15 - 1:21
    So it depends on whether jane is a person or an employee
  • 1:21 - 1:24
    If on the other hand you have a generic function
  • 1:24 - 1:27
    then things work differently
  • 1:27 - 1:32
    jane is now just a normal argument and
  • 1:32 - 1:37
    the function internally checks the type of the argument
  • 1:37 - 1:40
    and then depending on the type of the argument
  • 1:40 - 1:46
    chooses between two different implementations.
  • 1:46 - 1:49
    The same principle can be used if you have more than one parameter
  • 1:50 - 1:54
    and naturally you then have to check the type of each
  • 1:54 - 1:57
    of those two parameters.
  • 1:57 - 2:02
    So if you implemented multiple dispatch as a library in JavaScript
  • 2:02 - 2:05
    this is what it would look like.
  • 2:05 - 2:10
    You would first create a generic function that's called plus
  • 2:10 - 2:12
    then you would add two methods
  • 2:12 - 2:19
    and finally you would invoke it as you would with any other function
  • 2:19 - 2:22
    and then the types of the parameters
  • 2:22 - 2:26
    determine which of the two implementations are invoked when you make the call
  • 2:26 - 2:32
    as you can see it's like a table that maps function types
  • 2:32 - 2:38
    or parameters types to methods.
  • 2:38 - 2:41
    What are the benefits? Why do you do that?
  • 2:41 - 2:43
    What's good about it?
  • 2:43 - 2:45
    So what's actually happens is
  • 2:45 - 2:47
    you get more symmetry
  • 2:47 - 2:51
    Normally, the implicit parameter "this" is more powerful
  • 2:51 - 2:54
    than the other explicit parameters when you have
  • 2:54 - 2:56
    single dispatch like in JavaScript
  • 2:56 - 3:04
    So that's one of the properties of multiple dispatch
  • 3:04 - 3:07
    And then functions also become object-oriented
  • 3:07 - 3:11
    they become aware of the type hierachy of objects
  • 3:11 - 3:15
    The benefits are that whenever you have a polymorphic algorithm
  • 3:15 - 3:21
    that applies to several objects, then you can put that into a generic function
  • 3:21 - 3:24
    and here you have the generic function foo
  • 3:24 - 3:29
    it works with three objects and then it's obvious that foo
  • 3:29 - 3:34
    doesn't belong to a single object
  • 3:34 - 3:36
    it belongs to all of the objects at the same time.
  • 3:36 - 3:38
    So generic functions allow you to do that
  • 3:38 - 3:42
    and you have the visitor pattern for
  • 3:42 - 3:45
    single dispatch languages and that is actually just
  • 3:45 - 3:49
    a generic function implemented as an object
  • 3:49 - 3:54
    an you use single dispatch in the host to trigger the right visitor method
  • 3:54 - 4:00
    and languages with multiple dispatch include
  • 4:00 - 4:05
    Common Lisp, Haskell, R, Groovy, Clojure and C#
  • 4:05 - 4:09
    That's it, thank you!
Title:
dotJS 2012 - Axel Rauschmayer - Multiple dispatch
Description:

Filmed in Paris on Nov 30th, 2012. More talks on http://dotconferences.eu
Slides: http://goo.gl/EIR40

more » « less
Video Language:
English
Duration:
04:17
Sylvain Zimmer added a translation

English subtitles

Revisions