Repository navigation
Description
(google translate)
I am writing a jellyfin plugin in F#. Translate the sample code from C# to F#.
The number of lines of code is reduced by 50%, and the logic is clearer. F# is the best language in .Net. ❤
But when I implemented MediaBrowser.Controller.Providers.IRemoteMetadataProvider<'TItemType,'TLookupInfoType>, a compilation error FS0001 occurred.
I simplified the code and found that I cannot solve this problem directly.
Repro steps
- Create a C# class library and compile this:
public class BaseItem : IGetInfo<BaseItemInfo> { }
public class BaseItemInfo { }
public class ItemA : BaseItem, IGetInfo<ItemInfoA> { }
public class ItemInfoA : BaseItemInfo { }
public interface IGetInfo<out T> where T : BaseItemInfo { }
public interface IProvider<T, in U>
where T : BaseItem, IGetInfo<U>
where U : BaseItemInfo { }Expected behavior
- If Implementing IProvider<ItemA, ItemInfoA> by C#, the following code will compile succeed.
public class ImplementProvider : IProvider<ItemA, ItemInfoA> { }Actual behavior
- But when using F#, a compilation error occurred:
type ImplementProvider() =
interface IProvider<ItemA, ItemInfoA>error FS0001: This expression was expected to have type� 'BaseItemInfo' �but here has type� 'ItemInfoA'
error FS0909: All implemented interfaces should be declared on the initial declaration of the type
Known workarounds
I will try to implement IProvider<ItemA, BaseItemInfo> and explicitly convert the member parameter BaseItemInfo to ItemInfoA, and then test whether it works. It will most likely fail if jellyfin has a code that specifies the use of IProvider<ItemA, ItemInfoA>.
Related information
- I tried define the IProvider<T, U> by F#. A warning FS0064 and error FS0663 occurred
type IProvider<'T, 'U
when 'T:> BaseItem and 'T :> IGetInfo<'U>
and 'U:> BaseItemInfo> = interface endwarning FS0064: This construct causes code to be less generic than indicated by the type annotations. The type variable 'U has been constrained to be type 'BaseItemInfo'.
error FS0663: This type parameter has been used in a way that constrains it to always be 'BaseItemInfo'.
It may be that the F# compiler adds covariance and contravariance to the 'U at the same time, so a derived class of BaseItemInfo cannot be passed into 'U.
- Compilation succeeded after removing BaseItem. I don't know the reason.
public class BaseItemInfo { }
public class ItemA : IGetInfo<ItemInfoA> { }
public class ItemInfoA : BaseItemInfo { }
public interface IGetInfo<out T> where T : BaseItemInfo { }
public interface IProvider<T, in U>
where T : IGetInfo<U>
where U : BaseItemInfo { }- Operating system: Windows 10 Pro 21H1 (19043.1237)
- .NET Runtime kind: .NET 5.0.401
- Editing Tools: Microsoft Visual Studio Community 2019 Version 16.11.3


Activity
[-]Can't Implementing a generic interface define by C# where has special constraints with variance.[/-][+]Can't implementing a generic interface define by C# where has special constraints with variance.[/+]vzarytovskii commentedon Nov 4, 2021
Related: fsharp/fslang-suggestions#162
dsyme commentedon Apr 20, 2022
The language suggestion covers this