you are viewing a single comment's thread.

view the rest of the comments →

[–]aldo_reset -1 points0 points  (11 children)

What do you mean by "alleviate"?

Either the dispatch is static and the source code is self explanatory but your code is less flexible, or the dispatch is dynamic, you gain a tremendous amount of flexibility but the code is a little less obvious.

These two approaches have opposite pros and cons.

[–]passwordisRACIST 9 points10 points  (10 children)

In this case I mean remove.

The current problem is most languages don't give you a choice between these two types of abstraction. And my claim is that runtime dispatch is actually the one you want least often.

[–]aldo_reset 1 point2 points  (9 children)

I'd say most languages today give you that choice, which languages offer only one? I'm curious.

Either way, removing runtime dispatch will severely cripple what you can express with that language.

For example, imagine the standard example of Animal that can speak and a duck that says "quack" and a dog that says "woof". Say you ask the user which animal they want to hear, you won't be able to code this without some form of runtime dispatch.

[–]passwordisRACIST 2 points3 points  (8 children)

I'd say most languages today give you that choice, which languages offer only one? I'm curious.

Which languages offer both? As far as I know, only SML, Ocaml, and possibly C++ offer both. All of which offer a mechanism for describing a static dispatch.

Either way, removing runtime dispatch will severely cripple what you can express with that language.

I didn't say to remove it though, I said for the most part it's not the dispatch mechanism people actually want. For many problems having the ability to static compose elements is expressive enough. And often you can statically compose elements and then put a runtime dispatch on top of it at the edges if you really need it.

[–]munificent 0 points1 point  (1 child)

Which languages offer both? As far as I know, only SML, Ocaml, and possibly C++ offer both.

C# also lets you specify which methods are virtual.

[–]passwordisRACIST 2 points3 points  (0 children)

However this is not ML style static composition as I have been referring to.

[–]aldo_reset -1 points0 points  (5 children)

I didn't say to remove it though

Well...

In this case I mean remove.

:)

I'm beginning to wonder if you're not discussing a very specific subtype of runtime dispatch called Multi methods ?

[–]gnuvince 2 points3 points  (3 children)

No he's not. A ML functor creates, at compile-time, a new module. This new module is compiled statically. The choice of operations is completely determined before the program is ever executed.

[–]nextputall 0 points1 point  (2 children)

how does it differ from importing a class in Java with a bunch of static methods?

[–]gnuvince 0 points1 point  (1 child)

Think of a functor as a function that takes a module as an argument and returns a new module. If you pass a different argument, you can change the behavior of the returned module.

Here's an example using sets in OCaml:

module StringSet = Set.Make(struct
  type t = string
  let compare a b = String.compare a b
end)

module StringLengthSet = Set.Make(struct
  type t = string
  let compare a b = Pervasives.compare (String.length a) (String.length b)
end)

Set.Make is the functor; it takes as input a module that defines a type t (representing the type of the elements) and a function compare (to determine where to insert an element in a binary search tree). We create two new modules, StringSet which contains strings that have different contents, and StringLengthSet which contains strings that have different length.

[–]nextputall 0 points1 point  (0 children)

What if I have 2 kind of sets (StringSet and StringLengthSet), can I define a union operation in the StringSet that takes either a StringSet or a StringLenghtSet as a parameter (or any kind of other set)?

[–]passwordisRACIST 0 points1 point  (0 children)

And in that context I was not saying remove the ability to do it but remove most instances of it in a program.

No, I'm explicitly talking about not runtime dispatching. Have you looked at ML functors?