Skip to content

It is no longer possible to implement the same trait for different closures since they became traits #20770

Closed
@Nercury

Description

@Nercury

This worked before. But now there is no way to create different implementations for different closures - meaning this kind of use case goes away.

// Completely contrived example incomming

trait TwoArgClosure<A1, A2, T> {
    fn run_self(&self, a1: A1, a2: A2) -> T;
}

// I can implement this silly trait for 2 arg closure and everything gets
// matched just fine
impl<A1, A2, T, C> TwoArgClosure<A1, A2, T> for C where C: Fn(A1, A2) -> T {
    fn run_self(&self, a1: A1, a2: A2) -> T {
        (self)(a1, a2)
    }
}

// However, the following closure type is considered by compiler to be 
// the same - and I get "conflicting implementations" error
impl<A1, T, C> TwoArgClosure<A1, A1, T> for C where C: Fn(A1) -> T {
    fn run_self(&self, a1: A1, a2: A1) -> T {
        (self)(a1)
    }
}

// Note that leaving out EITHER one of these works... but not both

fn main()
{
    let test_a2 = |&: _: i32, b: i32| b;
    assert_eq!(3, test_a2.run_self(2, 3));

    let test_a1 = |&: a: i32| a;
    assert_eq!(2, test_a1.run_self(2, 3));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions