> Correct me if I am wrong, but in a generic typeclass system one can define a
> new type is an instance of (for example) Number by defining its '<', '+',
> etc.

Right. The Number typeclass would have a bunch of operations, and you
can declare a type to be an instance of this class by defining, in the
instance declaration, these operations for that type.

> Since we intend to support separate compilation, this looks like a really
> expensive feature.

I'm not sure what you are getting at. Each instance declaration would
lead to a vtable, so a type can have multiple vtables for the
different typeclasses it belongs to, but this is linear, and not
something to be worried about at all. The vtable will live in the
crate that declares the instance. In Haskell, and this is probably
worth following, only the module that defines the type, or the module
that defines the typeclass, can declare an instance for a
type/typeclass combo. So if your crate can 'uses' (as in, links) a
given typeclass and type, it also see any instance declarations for
them.

Then, as in your example, when a function passes an int to a function
one of whose type parameters is restricted to type Number, that is the
point where the vtable for int/Number is looked up, and passed along.
As an optimization, you can make this vtable point to the int type, so
that you don't have to pass both the vtable and the type descriptor to
the polymorphic function.

Let me know if that doesn't make sense.
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to