rust - Nested trait types, bounded types and verbosity in use of the as keyword -
i have question regarding use readability , verbosity of as keyword nested types within traits.
i'm coming c++ background , suspect misunderstanding come lack of knowledge of type systems used haskell, understand rust makes use of.
in c++, templated structs used traits, types defined based on template parameters. nesting can occur other struct types defined within structs etc. etc.
rust seems support similar functionality. struct mystruct<f: foo>
part of larger code snippet below, defines members based on types in supplied type f
, bounded trait foo
:
struct mystruct<f: foo> { // why not work? // surely f, being constrainted foo, // has type bar, constrained bar<f>, // has type baz, constrained baz<f> // data: vec<f::bar::baz>, data: vec< <<f foo>::bar bar<f>>::baz >, }
briefly, when defining members in struct, seems 1 has use <<f foo>::bar bar<f>>::baz
provide compiler type information. seems reasonable since compiler must know type of f
in order reason types. seems me information provided bounds placed on these types, within generic parameter struct, , within trait definitions themselves.
i find <<f foo>::bar bar<f>>::baz
bit difficult read compared f::bar::baz
, wondering if there anyway handle better code readability point of view? full code snippet below:
use std::vec::{vec}; pub trait foo { type value; type bar: bar<self>; } pub trait bar<f: foo> { type baz: baz<f>; } pub trait baz<f: foo> { fn new(value: f::value) -> box<f::value> { box::new(value) } } fn main() { struct barimpl; impl<f: foo> bar<f> barimpl { type baz = bazimpl; } struct fooimpl; impl foo fooimpl { type value = f64; type bar = barimpl; } struct bazimpl { dummy: i32 }; impl<f: foo> baz<f> bazimpl {}; struct mystruct<f: foo> { // why not work? // surely f, being constrainted foo, // has type bar, constrained bar<f>, // has type baz, constrained baz<f> // data: vec<f::bar::baz>, data: vec< <<f foo>::bar bar<f>>::baz >, } let mut s = mystruct::<fooimpl> { data: vec::new() }; x in 0..5 { let b = bazimpl{ dummy: x}; s.data.push(b); } println!("s.data.len() = {}", s.data.len()); }
the first level can implied, can have vec<<f::bar bar<f>>::baz>
. it’s quite possible in future rules loosened can cope inferring appropriate constraints multiple levels, it’s not case now.
Comments
Post a Comment