# Specialization, associated types (again?) and type alias impl taits

**URL:** <https://internals.rust-lang.org/t/specialization-associated-types-again-and-type-alias-impl-taits/15058>\
**Category:** language design\
**Created:** [July 22, 2021, 3:56am UTC](https://internals.rust-lang.org/t/specialization-associated-types-again-and-type-alias-impl-taits/15058 "2021-07-22T03:56:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![uzytkownik](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/uzytkownik/32/5013_2.png) [@uzytkownik](https://internals.rust-lang.org/u/uzytkownik)\
**Post date:** [July 22, 2021, 3:56am UTC](https://internals.rust-lang.org/t/specialization-associated-types-again-and-type-alias-impl-taits/15058/1 "2021-07-22T03:56:16Z")

</div>

I'm playing around the nightly rust and I run into a problem of providing default implementation for traits:

```rust
trait Trait {
    type Foo<'a>: Future<Output = u32>;
    fn foo<'a>(&'a self) -> Self::Foo<'a>;
    type FooMany<'a>: Future<Output = ()>;
    fn foo_many<'a>(&'a self, out: &mut [u32]) -> FooMany<'a>;
    type Bar;
    fn bar(&self, bar: Self::Bar);
}

```

It would be nice if `foo_many` could be provided with default implementation:

```rust
default trait Tait {
    type FooMany<'a> = impl Future<Output = ()>;
    fn foo_many<'a>(&'a self, out: &mut [u32]) -> FooMany<'a> {
        for elem in out.iter_mut() {
            *elem = self.foo().await;
        }
    }
    type Bar = Void;
    fn bar(&self, bar: Self::Bar) {
        unreachable(bar);
    }
}

```

This won't work as `Self::FooMany` is still considered unknown by compiler. Are there any plans to lift the restriction so that such code is allowed?

I would assume that if the restriction is lifted the code:

```rust
impl Trait for u32 {
    type Foo<'a> = impl Future<Output = u32>;
    fn foo<'a>(&'a self) -> Self::Foo<'a> {
        async move {
            *self
        }
    }
    type FooMany<'a> = ...;
    type Bar = something other than void;
}

```

Would fail with appropiate error.

I will also note that Bar can be workaround by:

```rust
trait TraitAux {
    type Bar;
}
trait Trait {
    type Foo<'a>: Future<Output = u32>;
    fn foo<'a>(&'a self) -> Self::Foo<'a>;
    type FooMany<'a>: Future<Output = ()>;
    fn foo_many<'a>(&'a self, out: &mut [u32]) -> FooMany<'a>;
    type Bar;
    fn bar(&self, bar: Self::Bar);
}

default impl<T> TraitAux for T {
   type Bar = Void;
}

default impl<T: Trait<Bar = Void>> Trait for T {
    fn bar(&self, bar: Self::Bar) {
        unreachable(bar);
    }
}

```

But it doesn't work for `impl Trait`.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [July 23, 2021, 4:09am UTC](https://internals.rust-lang.org/t/specialization-associated-types-again-and-type-alias-impl-taits/15058/2 "2021-07-23T04:09:01Z")

</div>

You could try using [existential types](https://rust-lang.github.io/rfcs/2071-impl-trait-existential-types.html). This question is better directed towards [users.rust-lang.org](http://users.rust-lang.org). Internals is for discussions around the design and implementation of The Rust Programming Language.

---

<div class="post-metadata">

**Author:** ![uzytkownik](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/uzytkownik/32/5013_2.png) [@uzytkownik](https://internals.rust-lang.org/u/uzytkownik)\
**Post date:** [July 24, 2021, 12:15am UTC](https://internals.rust-lang.org/t/specialization-associated-types-again-and-type-alias-impl-taits/15058/3 "2021-07-24T00:15:45Z")

</div>

Sorry. I will in future.

Anyway - existential types don't seem to be implemented yet?

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [July 24, 2021, 1:44am UTC](https://internals.rust-lang.org/t/specialization-associated-types-again-and-type-alias-impl-taits/15058/4 "2021-07-24T01:44:04Z")

</div>

They should be implemented on nightly... what did you try?

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mbrubeck/32/174_2.png) [@mbrubeck](https://internals.rust-lang.org/u/mbrubeck)\
**Post date:** [July 24, 2021, 2:11am UTC](https://internals.rust-lang.org/t/specialization-associated-types-again-and-type-alias-impl-taits/15058/5 "2021-07-24T02:11:00Z")

</div>

The syntax implemented on nightly is different from the original RFC. It's described in [RFC 2515 (`impl Trait` in type aliases)](https://github.com/rust-lang/rfcs/blob/master/text/2515-type_alias_impl_trait.md). It requires the unstable [`#![feature(type_alias_impl_trait)]`](https://github.com/rust-lang/rust/issues/63063).

---

<div class="post-metadata">

**Author:** ![uzytkownik](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/uzytkownik/32/5013_2.png) [@uzytkownik](https://internals.rust-lang.org/u/uzytkownik)\
**Post date:** [July 24, 2021, 3:31am UTC](https://internals.rust-lang.org/t/specialization-associated-types-again-and-type-alias-impl-taits/15058/6 "2021-07-24T03:31:04Z")

</div>

Thanks. I tried several variation of the syntax. Anyway - that doesn't seems to solve the issue from the original post as I already use `min_type_alias_impl_trait`?

I tried several iterations but I fail to see how it solves default implementation as I was only able to do it in impl block and it doesn't seem to lift restrictions?

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 22, 2021, 3:31am UTC](https://internals.rust-lang.org/t/specialization-associated-types-again-and-type-alias-impl-taits/15058/7 "2021-10-22T03:31:54Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
