Rant after trying Rust a bit

Tobias Müller via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 27 12:21:10 PDT 2015


"Jonathan M Davis" <jmdavisProg at gmx.com> wrote: 
> On a related note, while I'd noticed it on some level, I don't think that
> it had ever clicked for me how restrictive interfaces are before this
> discussion. The simple fact that you can't ask for two of them at once
> really reduces how reusable your code can be. So, templatizing those
> checks rather than using interfaces is huge. And DbI is an extension of
> that. There's likely a lot of unplumbed depth there.

One big improvement of traits over interfaces is, that you can implement
traits for types after they are defined, even for types that you didn't
define yourself.

So in Rust, if your function needs two unrelated interfaces (trait objects
== dynamic polymorphism) A and B, you can easily define a new trait C that
depends on A and B and implement C for all types that also implement A and
B:

trait A {...}
trait B {...}

trait C : A,B { }

impl<T: A+B> C for T { }

fn myFunction(c: C) {...}

For generics you don't even need that:

fn myFunction<T: A+B>(t: T) {...}

Tobi


More information about the Digitalmars-d mailing list