Uh oh!
There was an error while loading. Please reload this page.
[RFC FS-1031] Discussion: Allow implementing the same interface at different generic instantiations in the same type #185
Replies: 11 comments
@dsyme Do I understand your comment correctly, that you explicitly don't want to enable the following scenario (multiple instantiations with type unknowns)? letx={new IGet<_>withmemberx.Get()=1interface IGet<_>withmemberx.Get()="hello"interface IGet<_>withmemberx.Get()=1.}I'm kind of torn, disallowing it would really simplify the implementation, but I think it would be nice to have.
ref: dotnet/fsharp#18 |
Yes, my gut feeling was that we should require full type instantiations at these points (or else there will be trouble further down the line in processing the declarations) I may be wrong though, will look at your prototype and tests |
I have for now updated the RFC to explicitly disallow multiple type unknowns - should we figure out that the implementation would be trivial (which I doubt ...), it can be added again. Besides, if the member implementations become slightly more complex than a single constant expression, it may be good design to explicitly state the type anyway. |
I just noticed that the following code typeIFoo<'T>=interfaceendletf<'Twhen'T:>IFoo<int>and'T:>IFoo<string>>()=()fails with the type error The error feels somewhat related to the current generic interfaces restriction. |
It is currently possible to override an interface implementation from a base class: typeIB<'a>=interfaceabstractX :unit->intendtypeCBase()=interface IB<int>withmemberx.X()=1typeC2()=inherit CBase()interface IB<int>withmemberx.X()=3It is currently not possible to override a specific interface implementation with a generic implementation: typeIB<'a>=interfaceabstractX :unit->intendtypeCBase()=interface IB<int>withmemberx.X()=1typeC2<'T>()=inherit CBase()interface IB<'T>withmemberx.X()=3The equivalent is possible in C#: interfaceI<T>{TGet();}publicclassBase:I<int>{publicintGet()=>1;}publicclassC<T>:Base,I<T>{publicTGet()=>default(T);}varc=newC<int>();>(casI<int>).Get()0This issue is not directly related to this RFC, but I found it when testing the behavior. I know that F# is not just a functional C#, so should it be considered "by-design", and a potential language suggestion, or should I move this to visualfsharp as a bugreport? What about the behavior @eiriktsarpalis found? "By design", so a new language suggestion, or bug, so a new bugreport at visualfsharp? |
The behaviour mentioned by @eiriktsarpalis is by design. From the F# language spec (4.0)
|
I believe all the behaviours here are currently by-design. So the suggestion can stay open here. |
@0x53A Will it work with F# struct (value) types too? Eg.: typeIA<'T>=abstractmemberGet :unit->obj[<Struct>]typeMyStruct=interface IA<int>withmemberx.Get()=upcast1interface IA<string>withmemberx.Get()=upcast2How it supposed to work / interop with default interface methods (https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md) ? |
@zpodlovics this is a pure language feature. It will not affect anything wrt default interface methods (which can be a pure CLR feature, but probably need language support to not be super annoying). Also, I see no reason why it shouldn't work with structs. |
I just discovered this from the list of features that may make it into F# 5. I hope is does. I think this may resolve a type issue I have been struggling with. jackfoxy/DependentTypes#14 |
This definitely gets a vote from me! I think it would be really useful to have as it increases the opportunities to use libraries written in other languages from F#. As I understand it, there is no workaround for this other than implementing in C# and reuse from F#. This was quite a surprise as it is the first time in the 4 years or so that I have been working in F# where I have encountered an interop issue that stops implementation in F#. My use case comes from is working with the Rebus .Net service bus. Anyone working with NServiceBus would also need this. Both of these libraries implement long-running, durable workflows for which I need to write something like this: |

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Discussion for https://github.com/fsharp/fslang-design/blob/master/FSharp-5.0/FS-1031-Allow%20implementing%20the%20same%20interface%20at%20different%20generic%20instantiations%20in%20the%20same%20type.md
All reactions