我想创建这个特征:
trait Functor<A> {
fn map<B, F>(self, f: F) -> Functor<B>
where
F: Fn(A) -> B;
}
我有此错误:
warning: trait objects without an explicit `dyn` are deprecated
--> src/lib.rs:2:33
|
2 | fn map<B, F>(self, f: F) -> Functor<B>
| ^^^^^^^^^^ help: use `dyn`: `dyn Functor<B>`
|
= note: `#[warn(bare_trait_objects)]` on by default
error: associated item referring to unboxed trait object for its own trait
--> src/lib.rs:2:33
|
1 | trait Functor<A> {
| ------- in this trait
2 | fn map<B, F>(self, f: F) -> Functor<B>
| ^^^^^^^^^^
|
help: you might have meant to use `Self` to refer to the implementing type
|
2 | fn map<B, F>(self, f: F) -> Self
| ^^^^
error[E0038]: the trait `Functor` cannot be made into an object
--> src/lib.rs:2:33
|
1 | trait Functor<A> {
| ------- this trait cannot be made into an object...
2 | fn map<B, F>(self, f: F) -> Functor<B>
| --- ^^^^^^^^^^ the trait `Functor` cannot be made into an object
| |
| ...because method `map` has generic type parameters
|
= help: consider moving `map` to another trait
似乎没有办法实现这种事情。
我宁愿返回类似Functor<B>
之类的内容,也不希望从map
方法中返回Self<B>
,因为我希望实现返回自身(使用通用B
而不是A
),但我不知道该怎么写。
起初,我尝试使用关联类型而不是通用类型,因为我认为它更合适,但效果并不更好。