On Fri, Jul 6, 2012 at 3:04 PM, Lindsey Kuper <lind...@rockstargirl.org> wrote:
> Two of the changes mentioned in the development roadmap that Graydon
> sent out earlier this week were "Extend interfaces to full traits" and
> "Enforce implementation coherence".  Those are addressed by this
> proposal for unifying traits and interfaces:
>
> https://github.com/mozilla/rust/wiki/Proposal-for-unifying-traits-and-interfaces

That last hash table example makes me uneasy because the proposed
solution doesn't really solve the problem very nicely at all.

If I have a 'hash' trait in a module Hash, and then two different hash
function providers FastHash (TM) and QuickHash (TM) want to provide
hash functions for use with a hash table, they can not actually be
used with any standard types, (without requiring first wrapping every
key inside some per-hash-function wrapper type. This is ugly and kinda
defeats the point of overloading - if we want to pick the
implementation explicitly at each use site, why not just pass in the
function dictionary explicitly instead of wrapping the arguments in a
special "marker" type whose sole purpose is to make the compiler pick
the right instance?

This seems.. quite painful. I would be extremely unlikely to use a
'hash' trait in a hash table implementation for this reason. I'd store
the hash function as a closure in the table itself, and set it up at
construction (this way you can pass in any hash function provider at
construction). It almost seems like the guidance for library writers
would be to avoid traits because they reduce the ability for users to
compose different behaviors (or at the very least each library would
provide two versions of everything - one which uses a trait, and one
which takes the method dictionary at construction time and stores it
with the data structure).

Not sure what the right solution is. I think Haskell's system is too
inflexible, but this is even less flexible. Maybe there are better
options in the literature somewhere? Maybe parameterizing modules is
the answer - the Hash module can define instances for pre-existing
types using functions that are passed in by the importer of the module
(the same module imported twice but parameterized differently would be
treated as two independent modules)?

-- 
Sebastian Sylvan
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to