【发布时间】:2015-10-25 00:10:13
【问题描述】:
试图让动态调度在 trait 静态方法中工作,但获取类型必须是已知的错误。
我正在尝试实现类似的目标
- F# https://github.com/Thorium/SimpleCQRS-FSharp/blob/master/CommandSide/Domain.fs
- C#
https://github.com/gregoryyoung/m-r/blob/master/SimpleCQRS/Domain.cs..
是使 trait 通用化的唯一方法吗?
pub struct Aggregate<T: AggregateRoot>
{
pub id: Uuid,
agg: T,
changes: Vec<Box<Any>>
}
impl <T :AggregateRoot > Aggregate<T>
{
fn GetUncomittedChanges(&self) -> Vec<Box<Any>> { self.changes}
fn MarkChangesAsCommitted(&self) { self.changes.drain(..);}
}
trait AggregateRoot
{
fn new2() -> Self; //should be private
fn new(id: Uuid) -> Self;
fn LoadsFromHistory(changes : Vec<Box<Any>> ) -> Self
where Self: Sized
{
let newAgg = AggregateRoot::new2 ();
changes.iter().map( |e| newAgg.Apply(e) );
newAgg.MarkChangesAsCommitted();
newAgg
}
fn Apply<U: Any>(&self, arg: U) ;
fn GetId(&self) -> Uuid;
}
目前正在尝试,但提供了 2 个参数,预计提供 1 个。
【问题讨论】:
-
呈现的代码有问题,没有描述的错误。相反,在编译代码后,我得到的错误是“没有为类型
Self实现特征core::marker::Sized”和“没有名为map的方法”。请创建一个MCVE,最好是在Rust Playground 上编译。 -
where 子句让你看到那个错误,但它看起来不对第二个错误是用 iter() 修复的。第一个错误是你是怎么做的。我不知道该怎么做才能让它编译。这是我目前的尝试
标签: rust