Re:Rust is surprisingly expressive
by frowaway001 on Saturday December 28, 2013 @08:10PM
(#6977590)
> One thing I wanted to do awhile back was create an interface that could only be implemented by a class that had already implemented certain other interfaces.
I think that's easily possible in Scala. I guess one wouldn't even need typeclasses for that.
This is essentially a Scala "self-type".
trait InterfaceA
trait InterfaceB { self: InterfaceA => }
class A extends InterfaceA // compiles
class B extends InterfaceB // does not compile
class B extends InterfaceA with InterfaceB // compiles
"self" isn't a keyword, you can call it whatever you like.Hah, now that is nice.
I forget what convoluted use case I wanted it for. I ended up solving it using composition instead (which is the right solution 99% of the time anyway. :) )
Still though, I love using interfaces as contracts, so I get frequently annoyed at how poorly my tools (limited to C++2003 right now) support doing such!
In retrospect, sort of a horrible idea, but I wonder, is there a type system out there expressive enough to allow that behavior?
After a few moments thought I realized why it wouldn't be possible in languages that use a fixed v-table, but it should be possible in languages that use more flexible calling techniques!
Or am I thinking about this too much and this is basically what type classes would buy me?