Hi all, again me )
I am wondering if the any proposal to simplify generics in such way ...
Rather than doing:
trait RunTimeReflection {
fn get_num_fields() -> usize;
}
trait IPerson {
fn get_first_name(&self) -> String;
fn get_second_name(&self) -> String;
}
struct Person {
first_name: String,
second_name: String,
}
impl IPerson for Person {
fn get_first_name(&self) -> String {
self.first_name.clone()
}
fn get_second_name(&self) -> String {
self.second_name.clone()
}
}
impl RunTimeReflection for Person {
fn get_num_fields() -> usize {
2
}
}
trait DebugPersonHolderReflection<T> where T: RunTimeReflection {
fn print(t: &Vec<T>);
}
struct PersonHolder<T> where T: Sized + IPerson {
persons: Vec<T>,
}
impl<T> DebugPersonHolderReflection<T> for PersonHolder<T>
where T: Sized + IPerson + RunTimeReflection {
fn print(t: &Vec<T>) {
for person in t {
println!("num_fields: {}, first_name: {}, second_name: {}",
T::get_num_fields(), person.get_first_name(), person.get_second_name());
}
}
}
it would be nice if compiler takes by default constraints from interface and automatically:
trait RunTimeReflection {
fn get_num_fields() -> usize;
}
trait IPerson {
fn get_first_name(&self) -> String;
fn get_second_name(&self) -> String;
}
struct Person {
first_name: String,
second_name: String,
}
impl IPerson for Person {
fn get_first_name(&self) -> String {
self.first_name.clone()
}
fn get_second_name(&self) -> String {
self.second_name.clone()
}
}
impl RunTimeReflection for Person {
fn get_num_fields() -> usize {
2
}
}
trait DebugPersonHolderReflection<T> where T: RunTimeReflection {
fn print(t: &Vec<T>);
}
struct PersonHolder<T> where T: Sized + IPerson {
persons: Vec<T>,
}
impl<T> DebugPersonHolderReflection<T> for PersonHolder<T> {
// where T: Sized + IPerson + RunTimeReflection
// (automatically get constraints from DebugPersonHolderReflection and PersonHolder)
fn print(t: &Vec<T>) {
for person in t {
println!("num_fields: {}, first_name: {}, second_name: {}",
T::get_num_fields(), person.get_first_name(), person.get_second_name());
}
}
}
It would be nice if the same works for trait generic method
We violate DRY principle due to this ...