I am trying to have some sort of list of types which I can then initialize and build genetic algorithms.
The issue is working with "types" themselves, I haven't found the right way to take an array of types and call ::new
on each to get an "instance".
// --- conditions.rs ----------------------------------
extern crate strum;
use strum_macros::{EnumIter}; // etc.
pub trait Testable {
fn new() -> Self;
fn test(&self, candle: &Candle) -> bool;
}
// use strum::IntoEnumIterator;
#[derive(EnumIter,Debug)]
pub enum Conditions {
SmaCheck,
CandlesPassed,
}
// simple moving average check
pub struct SmaCheck {
sma1: i8,
sma2: i8,
}
impl Testable for SmaCheck {
fn new() -> Self {
Self { sma1: 10, sma2: 20 }
}
fn test(&self, candle: &Candle) -> bool {
return true;
}
}
// --- generator.rs -----------------------------------
// creates random conditions, which will then be mutated and bred
use strum::IntoEnumIterator;
use crate::conditions::{Conditions, Testable};
pub fn run() {
for condition in Conditions::iter() {
println!("{:?}", condition); // this works
let testable = condition::new(); // undeclared type or module `condition`
println!("{:?}", testable::new());
}
}