Rant after trying Rust a bit

Tobias Müller via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 28 07:43:38 PDT 2015


"Jonathan M Davis" <jmdavisProg at gmx.com> wrote:
>> trait C : A,B { }
> 
> How is that any different from interfaces? You can do exactly the same thing with them.
> 
>> impl<T: A+B> C for T { }

^^^
That's the important line.
You can define *and implement* the trait C for *all types that already
implement A and B*.
That's definitely not possible with interfaces. The difference between
traits and interfaces is, that trait implementation is separate from the
type definition.

>> fn myFunction(c: C) {...}
> [...]
> But as soon as you have to create a new one that combines two or more
> interfaces/traits/concepts, then that doesn't scale. interfaces force
> that. I wouldn't expect traits or concepts to, because they're
> compile-time constructs, but that would depend on how the language defines them.
> 
> It might make sense to create combined traits/concepts for the cases
> where the operations in question are often going to be required together,
> but in general, it's going to scale far better to require them separately
> rather than require a combined trait/concept. Otherwise, you get a
> combinatorial explosion of traits/concepts as you combine them to create
> new traits/concepts - either that, or you code doesn't end up being very
> generic, because it's frequently using traits/concepts that require more
> operations than it actually uses, meaning that it will work with fewer
> types than it would otherwise.

Because you can implement the combined trait directly for all separate
subtraits, you can define that new trait just for the use of one function.

Tobi


More information about the Digitalmars-d mailing list