Skip to content

Can't implementing a generic interface define by C# where has special constraints with variance. #12206

Closed

Description

@jing8956

(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

  1. 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

  1. If Implementing IProvider<ItemA, ItemInfoA> by C#, the following code will compile succeed.
public class ImplementProvider : IProvider<ItemA, ItemInfoA> { }

Actual behavior

  1. 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 

image

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

  1. 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 end
warning 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'.

image

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.

  1. 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

  1. changed the title [-]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.[/+] on Oct 5, 2021
  2. vzarytovskii commented on Nov 4, 2021

    @vzarytovskii
    Member
  3. dsyme commented on Apr 20, 2022

    @dsyme
    Contributor

    The language suggestion covers this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions