Trying to play with feature(type_alias_impl_trait) without success

Hello,
Here is the problem:

#![feature(type_alias_impl_trait)]

use std::fmt::Display;

type SomeDisplay = impl Display;

fn make_it() -> SomeDisplay {
    2
}

struct Example {
    it: SomeDisplay,
}

impl Example {
    fn make() -> Self {
        Example { it: make_it() }
    }
}

The error tells:
SomeDisplay must be used in combination with a concrete type within the same crate

Thank you in advance for your help.

Use the define_opaque attribute everywhere you want to use the underlying type (which you have to do at least once).

+#[define_opaque(SomeDisplay)]
 fn make_it() -> SomeDisplay {
     2
 }

I thought rust was able to detect it was an opaque type...
:+1:

It turns out automatically deciding which items get to define an opaque (and thus know its underlying type) is tricky, with various trade-offs. For awhile there was a "must define if could define" rule that often required putting the TAIT and some subset of methods in a sub-module to limit the defining scope, for example.

There's more discussion here and probably in related issues too.

1 Like