What is proper way of declaring generic enum with several parameters?
use std::rc::Rc;
//
fn main()
{
let p1 = Expr::val( true );
println!( "p1 : {:?}", p1 );
let p2 = Expr::other( true );
println!( "p2 : {:?}", p2 );
}
//
#[derive( Debug )]
pub enum Expr< T = (), V : Copy = () >
{
Val( V ),
Other( Rc< T > ),
}
//
impl< T, V : Copy > Expr< T, V >
{
pub fn val( val : V ) -> Self
{
Expr::Val( val )
}
pub fn other( p : T ) -> Self
{
Expr::Other( Rc::new( p ) )
}
}
Why cant it deduce types?
error[E0282]: type annotations needed for `Expr<T, bool>`
--> src/main.rs:8:12
|
8 | let p1 = Expr::val( true );
| -- ^^^^^^^^^ cannot infer type for type parameter `T`
| |
| consider giving `p1` the explicit type `Expr<T, bool>`, where the type parameter `T` is specified
For more information about this error, try `rustc --explain E0282`.
error: could not compile `playground` due to previous error
If Expr::var() always returns Expr<(), T>, code like this will not compile:
let mut p1 = Expr::val(true); // compiler infers V = bool, but T is unknown
println!("p1 : {:?}", p1);
let p2 = Expr::other(true); // compiler infers T = bool, but V is unknown
println!("p2 : {:?}", p2);
// you assign p2 to p1, so p1 and p2 must be the same type, now compler
// can figure out that it must be Expr<boo, bool>
p1 = p2;