Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to type alias a specialised generic enum

Tags:

enums

rust

Given the following:

use std::fmt::Debug;

#[derive(Debug)]
enum A<T: Debug> {
    X,
    Y(T),
}

#[derive(Debug)]
struct B;

type C = A<B>;
// use A<B> as C; // Does not compile

I can use it as:

fn main() {
    let val0 = A::X::<B>;
    let val1 = A::Y::<B>(B);
    println!("{:?}\t{:?}", val0, val1);
}

But then for more than one generic parameter (or if A, B etc were much longer names then to alias it I tried the following but it doesn't compile:

fn main() {
    let val0 = C::X;
    let val1 = C::Y(B);
    println!("{:?}\t{:?}", val0, val1);
}

with errors:

src/main.rs:656:16: 656:20 error: no associated item named `X` found for type `A<B>` in the current scope
src/main.rs:656     let val0 = C::X;
                               ^~~~
src/main.rs:657:16: 657:20 error: no associated item named `Y` found for type `A<B>` in the current scope
src/main.rs:657     let val1 = C::Y(B);

As also noted i am unable to use use to solve the problem. Is there a way around it (because typing the whole thing seems to be cumbersome) ?

rustc --version
rustc 1.9.0 (e4e8b6668 2016-05-18)
like image 701
ustulation Avatar asked Jun 30 '16 12:06

ustulation


1 Answers

Is there a way around it (because typing the whole thing seems to be cumbersome)?

You can specify C as the type of the variable so you can use A::X or A::Y without explicit specifying the type parameter:

let val0: C = A::X;
let val1: C = A::Y(B);
like image 199
malbarbo Avatar answered Nov 02 '22 02:11

malbarbo