F # return ICollection

I am working with a library built in C #. I've been working on porting some code to F #, but have to use quite a few base types from the C # lib.

One piece of code is to calculate a list of values ​​and assign it to a public field / property in the class. Field is a C # class that contains two ICollection.

My F # code works fine and should return FQ Seq / List.

I have tried the following code snippets, each producing errors.

  • The return type of the F # item is a type called restoreList with the type of restore list
  • A public field in a class that is a class that contains two ICollection objects

    this.field.Collection1 = recoveries
    
          

This gives the error Expected type ICollection but has a list of recovery types

this.field.Collection1 = new ResizeArray<Recoveries>()

      

Provides the expected ICollection type, but ResizeArray

this.field.Collection1 = new System.Collections.Generic.List<Recoveries>()

      

Same errors as above expected by ICollection, but enter List

Any ideas? These operations seem to be valid from a C # perspective, and List / ResizeArray implements ICollection, so ... I'm confused how to assign a value.

I could change the type of the underlying C # library, but that might have different implications.

thank

+1


source to share


1 answer


F # doesn't do implicit casting like C #. Therefore, even if it System.Collections.Generic.List<'T>

implements an interface ICollection

, you cannot directly set some ICollection

-typed property on an instance System.Collections.Generic.List<'T>

.

The fix is ​​easy, but all you have to do is add an explicit transition to ICollection

in your ResizeArray<'T>

or System.Collections.Generic.List<'T>

before its assignment:

// Make sure to add an 'open' declaration for System.Collections.Generic
this.field.Collection1 = (recoveries :> ICollection)

      



or

this.field.Collection1 = (ResizeArray<Recoveries>() :> ICollection)

      

+5


source







All Articles