Re:Abstraction without overhead: traits in Rust
by rwj on Monday May 11, 2015 @03:56PM
(#9527441)
This is also how interfaces are implemented in Go.
[ Parent
| on HN ]
Re:Abstraction without overhead: traits in Rust
by nostrademons on Monday May 11, 2015 @04:06PM
(#9527492)
And typeclasses in Haskell as well.
Re:Abstraction without overhead: traits in Rust
by evanpw on Monday May 11, 2015 @05:17PM
(#9527911)
I don't think that's true. Dictionary-passing (or something equivalent) attaches the table of methods to the functions which take typeclass instances as arguments (with extra, hidden arguments), while Rust's Box<Trait> attaches the table of methods to the object pointer itself. So you can have a list of Box<Show> in Rust, but you can't have a list of Show in Haskell. You have to set up the extra indirection yourself using something like this: https://wiki.haskell.org/Existential_type#Dynamic_dispatch_m....
I'm not an expert in Rust or Haskell, though, so I welcome corrections.
Re:Abstraction without overhead: traits in Rust
by bodhi on Tuesday May 12, 2015 @02:10AM
(#9529833)
> but you can't have a list of Show in Haskell
I'm not sure exactly what you mean, but my naive interpretation is that you can:
Prelude> :t map show
map show :: Show a => [a] -> [String]
Re:Abstraction without overhead: traits in Rust
by kimundi on Tuesday May 12, 2015 @03:44AM
(#9530091)
That's not a list of Show, that's a list of a single type that instantiates Show.
The difference being that in your code you can only put in a single type at a time, eg [Int] or [String], but not both Int and String under the common Show interface type.
Re:Abstraction without overhead: traits in Rust
by bodhi on Tuesday May 12, 2015 @07:07AM
(#9530813)
Aha, thanks (to twic also). Now I get it!
Re:Abstraction without overhead: traits in Rust
by dbaupp on Tuesday May 12, 2015 @03:36AM
(#9530067)
I'm not exactly sure what they mean, since the following works, and is pretty much a drop-in replacement for a Box<...> trait object in Rust.
data Showable = forall a. Show a => Showable a
This allows for [Showable 1, Showable "foo", Showable 'x']
(It requires the ExistentialQuantification language feature.)
Re:Abstraction without overhead: traits in Rust
by evanpw on Tuesday May 12, 2015 @08:48AM
(#9531307)
Right, this is exactly what I meant by setting up the indirection yourself. The main difference seems to be that in Haskell, you have to write this code separately for each typeclass, and wrap/unwrap it manually. In Rust, you just change Show to &Show and you get dynamic dispatch with no extra code. Here's a gist of the two approaches: https://gist.github.com/evanpw/89d89aae1159c608c476. One other difference is that in the Haskell showStatic, static dispatch is not a guarantee, just an optimization.
Re:Abstraction without overhead: traits in Rust
by bodhi on Tuesday May 12, 2015 @10:21PM
(#9536383)
This works on single instances, but I'm trying to see how it plays out with lists of `Bar`. As far as I can see, you need to use `Box`: https://play.rust-lang.org/?code=struct%20Foo%3B%0Astruct%20...
Which seems to me to be effectively the same as `Barrable` in this case, although it's more generic.
Re:Abstraction without overhead: traits in Rust
by bodhi on Wednesday May 13, 2015 @03:05AM
(#9537279)
> As far as I can see, you need to use `Box`
Which is exactly what you said in the first place!
> So you can have a list of Box<Show> in Rust, but you can't have a list of Show in Haskell.
But isn't this comparing two different things? You can't have a list of trait/typeclass in either language (excuse the pseudo-syntax):
Rust: [Bar]
Haskell: [Bar a => a]
But you can (with some extensions in Haskell) have: Rust: [Box<Bar>]
Haskell: [Box Bar]