News for hackers. Stuff that matters.
Re:I Don't Use Classes by uryga on Thursday March 12, 2020 @06:16PM (#22562496)
rust has `dyn FooTrait`, which lets you do dynamic diapatch, similarly to `FooInterface x = foo();` in e.g. java. even Haskell has a way of doing this via existential types like `(forall a. FooTypeclass a => a)`.

both of these predominantly statically-dispatched langs added ways to do dynamic dispatch later on, which seems to go against "dynamic dispatch could be entirely dropped without losing to much expressiveness". i prefer static-by-default as well, but it looks like it's hard to do without sometimes.

(of course you can always roll your own vtable-ish thing, so technically you could drop DD from the language without loss of expressiveness, but then you'd likely get multiple incompatible libraries for that...)

and i'd guess that some sort of dynamic dispatch is unavoidable if you want to do some kind of plugin architecture

Re:I Don't Use Classes by CuriousSkeptic on Thursday March 12, 2020 @08:09PM (#22563417)
Are you sure that Haskell thing is dynamic? I was under the impression that the type you have there is resolved statically by finding a type class for a
Re:I Don't Use Classes by uryga on Thursday March 12, 2020 @09:02PM (#22563757)
existential types allow you to make it dynamic. see https://wiki.haskell.org/Existential_type, there's a section comparing it to OOP-style dynamic dispatch

simple example copied from the linked article:

  data Obj = forall a. (Show a) => Obj a

  xs :: [Obj]
  xs = [Obj 1, Obj "foo", Obj 'c']

  doShow :: [Obj] -> String
  doShow [] = ""
  doShow ((Obj x):xs) = show x ++ doShow xs
we can have a "heterogenous" list of `Obj`s, like a java `ArrayList<IPrintable>`. each `Obj` is basically a pointer to the actual value of some type T and a pointer to T's Show dictionary (vtable). Which is afaik analogous to how OOP languages do it