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

[Proposal] method overloading with respect to generic type constraints #962

Closed
ibrahimbensalah opened this issue Mar 2, 2015 · 4 comments
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue Verified

Comments

@ibrahimbensalah
Copy link

I find it hard to create fluent builder classes for hierarchical classes. As an alternative I find it easy to write builder extension methods, Consider the following example

public class A {}
public class B: A {}
public static class ABuilderExtensions { 
   public static T Init<T>(this T t) where T : A {
       // add something
       return t;
   }
}

Now we can write fluent build statements for A

A a = new A().Init();

And we can write the same for B

B a = new B().Init();

Unfortunately, there is a problem. If we want to add yet another Init builder extension for a different class, say class C, then a compilation error is reported.

public class A {}
public class B: A {}
public class C {}
public static class ABuilderExtensions { 
   public static T Init<T>(this T a) where T : A {
       // add something
       return a;
   }
   // this causes a compilation error
   public static T Init<T>(this T c) where T: C {
       return c;
   }
}

This could be solved if method overloading would respect the generic type constraints because really the two Init method are not Ambiguous.

@svick
Copy link
Contributor

svick commented Mar 2, 2015

I think this is mostly a duplicate of #250, point 2.

Though having the two overloads in the same class is not part of that issue, and I believe it would require CLR change.

@mburbea
Copy link

mburbea commented Mar 2, 2015

How would this scenario be disambiguated?

public class FooBar : IFoo, IBar
{
    public static T Foo<T>(T f) where T: IFoo {...}
    public static T Foo<T>(T f) where T: IBar {...}
}
...
var x = Foo(new FooBar());

@ibrahimbensalah
Copy link
Author

@mburbea your scenario could in no way be disambiguated, but my scenario could be. If it is ambiguous then it is ambiguous and if it is not it's not.

Compare your scenario with the following using 'normal' methods, which would also cause compilation errors, which is totally acceptable

public class FooBar: IFoo, IBar
{
    public static IFoo Foo(IFoo f) { ... }
    public static IBar Foo(IBar b) { ... }
}
....
Foo(new FooBar());

My scenario is more equivalent to something like this

public class Foo: IFoo {}
public class Bar: IBar {}
public class Builder
{
    public static IFoo Foo(IFoo f) { ... }
    public static IBar Foo(IBar b) { ... }
}
....
Foo(new Foo());

@gafter
Copy link
Member

gafter commented Mar 2, 2015

Closing as a dup of #250. The specific scenario cited here would require a CLR change.

@gafter gafter closed this as completed Mar 2, 2015
@gafter gafter added Resolution-Duplicate The described behavior is tracked in another issue Area-Language Design labels Mar 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue Verified
Projects
None yet
Development

No branches or pull requests

5 participants