-
Notifications
You must be signed in to change notification settings - Fork 21
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
Allow implementing the same interface at different generic instantiations in the same type [ RFC FS-1031 ] #545
Comments
@0x53A I agree this should be done, as long as the disadvantages tracked above are taken into account int the implementation |
@dsyme when looking at how C# implements this, I noticed that apparently, Methods don't need to be named uniquely in IL: ClassLibrary that is imported with alias=al namespace Namespace
{
public class Class
{
}
} Console App: extern alias al;
namespace Namespace
{
public class Class
{
}
}
namespace ConsoleApp1
{
public interface IA<T>
{
object Get();
}
public class MyClass : IA<int>, IA<string>, IA<al.Namespace.Class>, IA<global::Namespace.Class>
{
object IA<int>.Get()
{
return 1;
}
object IA<string>.Get()
{
return 2;
}
object IA<global::Namespace.Class>.Get()
{
return 3;
}
object IA<al.Namespace.Class>.Get()
{
return 4;
}
}
class Program
{
static void Main(string[] args)
{
var x = new MyClass();
System.Console.WriteLine((x as IA<al.Namespace.Class>).Get());
System.Console.WriteLine((x as IA<global::Namespace.Class>).Get());
System.Console.ReadLine();
}
}
} So unless fsc itself has issues with duplicated names, it may be as easy as just removing the check. I will try to create a prototype over the next few days / weeks / months / years =) |
RFC: under discussion |
I have a wish to do this (which isnt allowed in C# due to the possibility of unification).
Is this going to be valid? naively I would hope the unification issue would either be handled when constructing the type...i.e. IOr<int,int,string> isnt valid but IOr<int,char,string> is |
@MarkNicholls No, that is not possible in F# for the same reason. |
sorry, let me make it a bit more concrete. C# allows this
C# doesnt allow this
it complains
|
open System.Collections
open System.Collections.Generic
type C() =
interface IEnumerable<int> with
member this.GetEnumerator(): IEnumerator<int> =
raise (System.NotImplementedException())
interface IEnumerable<string> with
member this.GetEnumerator(): IEnumerator<string> =
raise (System.NotImplementedException())
interface IEnumerable with
member this.GetEnumerator() : IEnumerator =
failwith "yeet" So that shouldn't be an issue. The example you provided previously will not work for the same reasons it doesn't work in C#. The error is: |
Ok, IEnumerable<> was just a convenient example of inheriting an interface...I don't actually want to do it. So the bottom line is the scope of this doesnt extend to my latter example, hmmmm....should I open a suggestion to include that? or is that premature? |
I suppose it's worth adding a new suggestion. |
Closing out as completed for F# 5. |
Note: this is a duplicate, but the original proposal was reduced down to only enable inheritance of such types, not implementation in F#.
I would like to ask you to reconsider fully implementing this feature.
Allow implementing the same interface at different generic instantiations in the same type
I propose we ...
Allow to implement the same interface multiple times with different generic instantiations.
E.g.
The existing way of approaching this problem in F# is ...
The existing workaround is to create one extra level of inheritance per instantiation.
Pros and Cons
The advantages of making this adjustment to F# are ...
Feature parity with C#.
Reduce surprise for new developers.
Remove boilerplate inheritance chains caused by the workaround.
The disadvantages of making this adjustment to F# are ...
Quoting from UV:
Extra information
Estimated cost (XS, S, M, L, XL, XXL): As this contains changes to code generation and type inference, this is probably M-L, but I am not familiar enough with the code base.
Related suggestions:
Original SO: http://stackoverflow.com/questions/1464109/implementing-the-same-interface-at-different-generic-instantiations
Original UV: https://fslang.uservoice.com/forums/245727-f-language/suggestions/5663504
Original PR: dotnet/fsharp#18
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: