Returning a trait type with static dispatch

Emanuel

I'm trying to return a statically dispatched trait in a function declared in the same trait, but the compiler seems unable to map between the trait type and the concrete type.

Example:

trait Money {
    fn trade<M>(&self) -> M
        where M: Money;
}

impl Money for Dollar {
    fn trade<M>(&self) -> M
        where M: Money
    {
        Dollar { amount: self.amount / 2 }
    }
}

You can try out the complete example here: http://is.gd/alQGv0

DK.

The compiler's in the right here. What you've told it is that you're implementing a function trade which, for any M the caller cares to use (so long as it implements Money), you will return an instance of that M. And then you go and try to return a Dollar. What if M isn't Dollar?

What you probably want here, from context, is the following:

trait Money {
    fn trade(&self) -> Self;
}

struct Dollar {
    amount: usize,
}

impl Money for Dollar {
    fn trade(&self) -> Dollar
    {
        Dollar { amount: self.amount / 2 }
    }
}

fn main() {
    let x = Dollar { amount: 10 };
    println!("{}", x.trade().amount);
}

Here, the trade function is changed to always return the type it's implemented for; within the Money trait definition, that's Self. When you're talking about a specific implementation, it's the implementation type (so it's Dollar when you're implementing Money for Dollar).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Type trait for member template static function

From Dev

Type trait: Check if reference member variable is static or not

From Dev

Call static method from trait on generic type

From Dev

Type trait for member template static function

From Dev

How to initialize a static std::unordered_map of a type trait?

From Dev

Implement trait for trait with static function

From Dev

Implement trait for trait with static function

From Dev

Returning promise from dispatch

From Dev

Can I "Pimp my Library" on a parameterized trait with method returning this.type?

From Java

How to use dynamic dispatch in Rust for trait with generics?

From Dev

PHP: Override trait static method

From Dev

PHP: Override trait static method

From Dev

c++ lambda with static dispatch

From Dev

A type trait for std::array

From Dev

Refering to self type of a trait

From Dev

Type trait for trivial types

From Dev

Trait that extends a type argument

From Dev

The trait `A` is not implemented for the type `&'a A + 'a`

From Dev

The trait `A` is not implemented for the type `A`

From Dev

Refering to self type of a trait

From Dev

Type trait for trivial types

From Dev

Clojure mutimethods for dispatch by type

From Dev

Implementing Borrow<Trait> for a type that implements Trait

From Dev

Requiring a trait bound on the associated type of an inherited trait

From Dev

static routing returning 404?

From Dev

Returning the subclass in a UIViewController static

From Dev

Returning reference to trait rather than struct

From Dev

Unresolved name for Trait when specifying the Type (<Type as Trait>)

From Dev

Is-braces-constructible type trait

Related Related

HotTag

Archive