Let me clarify what I mean:
I have a struct called Agent, which would be used like so:
let agent : Agent<PolicyRandom>= Agent::new();
And the policy PolicyRandom
is a type having to implement TraitPolicy
.
trait TraitPolicy<T: TraitModel> {
const POLICY_NAME: &'static str;
const MODEL: T; // IMPORTANT
fn new() -> Self{}
fn train(&self);
}
The specific Policy:
struct PolicyRandom {
num: usize,
}
impl TraitPolicy<MyModel2> for PolicyRandom {
const POLICY_NAME: &'static str = "Earth";
const MODEL: MyModel2 = MyModel2 {};
...
}
Now here comes the problem when defining struct for Agent
:
struct Agent<T: TraitPolicy> {//ERROR: missing generics for trait `TraitPolicy` expected 1 generic argument
policy: T,
}
impl<T> Agent<T> {
pub fn new() -> Self {
Self { policy: T::new() }
}
}
The error tells me that I need to add signature for the model type I use. But this is not what I want---I don't want to add another ModelType when initialing an agent object with type other than PolicyType.
Someone may wonder: "Why using generic type with TraitPolicy
?"----Because I require every specific policyType has a field called model
implementing TraitModel
, while each specific policyType ONLY need ONE specifc model.
Therefore, if I change TraitPolicy
into this without generic:
trait TraitPolicy {
const POLICY_NAME: &'static str;
const MODEL: impl TraitModel;// syntax ERROR!
...
}
It is not compilable. I don't know How to solve it.
Also, actually, I don't care whether the field MODEL is constant or not. But It seems that I can't make it unconstant in Trait, syntaxlly.