Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple implementations of a generic interface on same type will have conflicting members #5954

Closed
AndrewBrownK opened this issue Dec 29, 2024 · 2 comments · Fixed by #5965
Assignees
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang

Comments

@AndrewBrownK
Copy link

AndrewBrownK commented Dec 29, 2024

I'm coming from rust and hoping to implement interfaces multiple times in this fashion. However, there seems to be a conflict. In rust, the different generic types (Dog, Bat, Fox) would make each implementation independent and non-interferring.

The associatedtype may or may not be required to reproduce the issue.

problem.slang

interface IFoo<T> {
    associatedtype Output;
    func foo(other: T) -> Output;
}

struct Ant {};
struct Bat {};
struct Cat {};
struct Dog {};
struct Ewe {};
struct Fox {};
struct Gnu {};

extension Ant: IFoo<Bat> {
    typedef Cat Output;
    func foo(other: Bat) -> Cat { return Cat(); }
}
extension Ant: IFoo<Dog> {
    typedef Ewe Output;
    func foo(other: Dog) -> Ewe { return Ewe(); }
}
extension Ant: IFoo<Fox> {
    typedef Gnu Output;
    func foo(other: Fox) -> Gnu { return Gnu(); }
}

slangc problem.slang -o problem.slang-module

problem.slang(18): error 38100: type 'Ant' does not provide required interface member 'foo'
extension Ant: IFoo<Dog> {
               ^~~~
problem.slang(3): note: see interface requirement declaration of 'foo'
    func foo(other: T) -> Output;
         ^~~
problem.slang(22): error 38100: type 'Ant' does not provide required interface member 'foo'
extension Ant: IFoo<Fox> {
               ^~~~
problem.slang(3): note: see interface requirement declaration of 'foo'
    func foo(other: T) -> Output;
         ^~~
@AndrewBrownK
Copy link
Author

Work around (may not work in all cases):

  • Use overloaded functions instead of an interface

@csyonghe csyonghe self-assigned this Dec 30, 2024
@csyonghe csyonghe added the goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang label Dec 30, 2024
@csyonghe csyonghe added this to the Q4 2024 (Fall) milestone Dec 30, 2024
@csyonghe
Copy link
Collaborator

This turns out to be an easy fix in the lookup logic to prefer decls declared in the same extension decl when looking up for candidates that implements Output associated type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants