0:00:09.658,0:00:13.157 Hello, so my talk is going to be on multiple dispatch. 0:00:13.157,0:00:17.441 First for starters what is method dispatch? 0:00:17.441,0:00:19.475 If you have a polymorphic method 0:00:19.475,0:00:22.292 that mean if you have multiple implementations 0:00:22.292,0:00:25.607 and we'll soon see an example to make it clear what I mean 0:00:25.607,0:00:31.507 if you have those multiple implementations, the dispatch determines 0:00:31.507,0:00:35.341 which of those implementations will be called when you 0:00:35.341,0:00:37.661 invoke the method. 0:00:37.661,0:00:42.496 JavaScript has simple dispatch, so a single value is used for dispatch 0:00:42.496,0:00:46.660 we will then look at something new 0:00:46.660,0:00:50.128 and that is called multiple dispatch. 0:00:50.128,0:00:55.161 You can either dispatch via an object or 0:00:55.161,0:00:58.811 via a function. And if you dispatch via an 0:00:58.811,0:01:00.861 object this is what it looks like. 0:01:00.861,0:01:03.960 You have a so-called polymorphic method 0:01:03.960,0:01:11.077 and the type of jane determines which of the two functions 0:01:11.077,0:01:14.927 will be called if you make that method call up here. 0:01:14.927,0:01:20.761 So it depends on whether jane is a person or an employee 0:01:20.761,0:01:24.345 If on the other hand you have a generic function 0:01:24.345,0:01:26.695 then things work differently 0:01:26.695,0:01:31.844 jane is now just a normal argument and 0:01:31.844,0:01:36.807 the function internally checks the type of the argument 0:01:36.807,0:01:40.093 and then depending on the type of the argument 0:01:40.093,0:01:45.619 chooses between two different implementations. 0:01:45.619,0:01:48.588 The same principle can be used if you have more than one parameter 0:01:49.943,0:01:53.626 and naturally you then have to check the type of each 0:01:53.626,0:01:56.842 of those two parameters. 0:01:56.842,0:02:02.116 So if you implemented multiple dispatch as a library in JavaScript 0:02:02.116,0:02:04.606 this is what it would look like. 0:02:04.606,0:02:10.238 You would first create a generic function that's called plus 0:02:10.238,0:02:12.471 then you would add two methods 0:02:12.471,0:02:18.988 and finally you would invoke it as you would with any other function 0:02:18.988,0:02:21.854 and then the types of the parameters 0:02:21.854,0:02:26.219 determine which of the two implementations are invoked when you make the call 0:02:26.219,0:02:32.188 as you can see it's like a table that maps function types 0:02:32.188,0:02:38.188 or parameters types to methods. 0:02:38.188,0:02:41.404 What are the benefits? Why do you do that? 0:02:41.404,0:02:42.955 What's good about it? 0:02:42.955,0:02:45.205 So what's actually happens is 0:02:45.205,0:02:46.938 you get more symmetry 0:02:46.938,0:02:50.739 Normally, the implicit parameter "this" is more powerful 0:02:50.739,0:02:53.622 than the other explicit parameters when you have 0:02:53.622,0:02:56.021 single dispatch like in JavaScript 0:02:56.021,0:03:04.087 So that's one of the properties of multiple dispatch 0:03:04.087,0:03:06.872 And then functions also become object-oriented 0:03:06.872,0:03:10.956 they become aware of the type hierachy of objects 0:03:10.956,0:03:14.622 The benefits are that whenever you have a polymorphic algorithm 0:03:14.622,0:03:20.699 that applies to several objects, then you can put that into a generic function 0:03:20.699,0:03:23.784 and here you have the generic function foo 0:03:23.784,0:03:28.867 it works with three objects and then it's obvious that foo 0:03:28.867,0:03:33.599 doesn't belong to a single object 0:03:33.599,0:03:36.067 it belongs to all of the objects at the same time. 0:03:36.067,0:03:38.034 So generic functions allow you to do that 0:03:38.034,0:03:41.832 and you have the visitor pattern for 0:03:41.832,0:03:45.183 single dispatch languages and that is actually just 0:03:45.183,0:03:49.183 a generic function implemented as an object 0:03:49.183,0:03:54.483 an you use single dispatch in the host to trigger the right visitor method 0:03:54.483,0:03:59.883 and languages with multiple dispatch include 0:03:59.883,0:04:05.216 Common Lisp, Haskell, R, Groovy, Clojure and C# 0:04:05.216,0:04:09.049 That's it, thank you!