Characteristic of merging with related types

Is it possible to create an alias for features with the specified related types? I am using a method from a similar question Alias ​​type for several attributes

trait Trait { type Item; } fn print<T>(value: T) where T: Trait<Item=char> { } trait Alias: Trait {} impl<T: Trait<Item=char>> Alias for T {} fn print_alias<T: Alias>(value: T) { print(value) } fn main() { } 

However, it cannot compile with the following error:

 <anon>:12:5: 12:10 error: type mismatch resolving `<T as Trait>::Item == char`: expected associated type, found char [E0271] <anon>:12 print(value) ^~~~~ <anon>:12:5: 12:10 note: required by `print` <anon>:12 print(value) ^~~~~ error: aborting due to previous error 

Link to play: http://is.gd/LE4h6a

+7
rust
source share
3 answers

The @Shepmaster solution solves the problem locally; but you will need to specify where T: Alias<Item=char> each time. Alternatively, you can solve it globally, requiring all Alias implement Trait<Item=char> :

 trait Alias: Trait<Item=char> {} impl<T: Trait<Item=char>> Alias for T {} 

Which of the global or local solutions is preferred is entirely up to you.

+4
source share

Currently, it is only required that all Alias values ​​must implement Trait , but not that the Item type must be char . To do this, you need to use the following:

 trait Alias: Trait<Item=char> {} 
+3
source share

You still need to specify the associated type in the print_alias method:

 fn print_alias<T>(value: T) where T: Alias<Item=char> { print(value) } 

The problem is that you indicated that each Trait<Item=char> also implements Alias (and therefore Alias<Item=char> ), but that does not mean that every Alias implements Trait<Item=char> !

+2
source share

All Articles