F# converting Seq to ICollection - f#

I have a seq of types
let rackStatusesPrototype : RackStatus seq
and now I want to assign it to a property which is from c# and is an ICollection type, but I am having a problem with it. I tried:
let test : ICollection<RackStatus> = rackStatusesPrototype :> ICollection<RackStatus>
But it says incompatible types. What can i do?

The simplest thing you can do is to open System.Linq and invoke ToList() on your rackStatusesPrototype to have it converted to C# list, which implement ICollection.
Also F# Array implements ICollection, so rackStatusesPrototype |> Array.ofSeq should work as well. This may be preferred way, if you are having no other interactions with C# world.
Note, that F# list/set/map currently doesn't implement ICollection interface, but adding it is planned.

Related

How do I create a type that hosts a list of items, that implement an interface, but have different typed parameters?

How do I create a type that hosts a list of items, that implement an interface, but have different typed parameters?
Specifically, I need a container to hold a list of interfaces that have typed parameters that vary.
Note:
The types are not predetermined. Thus, I don't think I can use discriminated unions for generic type parameters that I can't foresee.
Is it possible?
let syncItem1 = { someSyncItem1 with Subscribers = seq {someResponder1} }
let syncItem2 = { someSyncItem2 with Subscribers = seq {someResponder2} }
let engines = seq [Engine(seq {syncItem1}) :> IEngine<int,string>
Engine(seq {syncItem2}) :> IEngine<string,int> // Compile error
] |> MultiEngine
Error:
All elements of a list must be of the same type as the first element, which here is 'string'. This element has type 'int'.
Appendix:
GitHub
You don't. IEngine<int, string> and IEngine<string, int> are, on a type-theoretical level, completely unrelated types and putting them in the same list makes no sense.
If there is a reasonable way that an engine can be used regardless of its parameters, extract that functionality into a parameterless superinterface and make the list typed to that.
If there is no reasonable way to do that, you can't do polymorphic dispatch anyway, so you need type tests and might as well use a list of Object too.

Passing a list from C# to F#, how to specify the parameter is a list?

With the following C# code
var MyList = new List<MyClass>();
SomeFSharpFunc(MyList);
on the F# side:
let SomeFSharpFunc (MyList: ???What do I put here so I know it's a list) =
I don't know to how express that the incoming parameter is a list.
Also, do I need to duplicate the C# class as a F# type?
You can just use
open System
let SomeFSharpFunc (MyList: System.Collections.Generic.List<MyClass>) = ...
but you should consider using seq<MyClass> instead (which is equivalent to IEnumerable<MyClass>) if you don't need full power of System.Collections.Generic.List, especially since in F# "list" generally refers to a very different data structure.
See also Converting between C# List and F# List.
Also, do I need to duplicate the C# class as a F# type?
No.

Delay the implementation of interface methods?

I've been programming in F# for some years and there's an "issue" that's been bothering me for some time and I have not been able to solve. It is not a bug, I think it is a design decision, but anyway, the problem is this: is there a way to delay (maybe that's not the correct word for this) the implementation of interfaces?, that is, not implementing them in the initial definition, but later, maybe in the same file after I have implemented a module for the type. I'll explain with a simplified example:
Suppose I have the following data structure:
type 'T MyCollection =
(*type definition*)
interface IEnumerable<'T> with
member this.GetEnumerator () =
(* I don't want to implement it here
because I still don't have the module
with a toSeq function *)
If I implemented the method right there, I would have to also implement all the functions as methods of the type and then the module would be just a "proxy" for calling the methods. This way I'm creating a OO-first data structure and then creating a module (overloaded with type annotations) to allow for a functional-first usage. I would prefer to write a functional-first data structure (cleaner since the type inference can work better) and then create a OO wrapper to allow a better intellisense support for languages like C#. That approach complies with what the design guidelines for F# tells us, but the interfaces can't be implemented anywhere but in the initial definition of the type. That restriction forces me to write the whole data structure with members.
I've been looking for examples and I've found that the list implementation in FSharp.Core list does exactly what I want, but I can't do that, the compiler won't let me.
I'm almost sure that this is a design decision, maybe to avoid encouraging bad practices, I don't know, but I don't consider my wish to be a bad practice. Also I'm well aware of the linear nature of the fsharp compiler.
Please if any of you know how to do what I want, I'll be glad if you tell me. Also, if any of you know why I should not follow this approach I'll be glad to know too. There must be a reason why this is not a problem for anyone else.
Thanks in advance.
I completely agree that this is unfortunate problem. The trick that is used in the source code of 'a list in the F# Core library is to define the implementation of the interface in a type augmentation. The compiler does not complain when you add members to a type in this way, but it says that adding implementation of an interface in this way is deprecated. However, it does not prevent you from doing this. The following compiles fine for me:
open System.Collections
open System.Collections.Generic
type MyCollection<'T> =
{ Data : 'T list }
interface IEnumerable<'T>
interface IEnumerable
let getEnumerator { Data = d } =
(d :> seq<_>).GetEnumerator()
type MyCollection<'T> with
interface IEnumerable<'T> with
member this.GetEnumerator() = getEnumerator this
interface IEnumerable with
member this.GetEnumerator() = (getEnumerator this) :> _
The fact that this is deprecated is a bit unfortunate. I quite like this style and I use it when it makes sense. You can start a discussion about this on F# user voice and perhaps it could be turned back into a normal accepted feature :-)

F# return ICollection

I'm working with a library created in C#. I've been working on porting some code to F# but must use quite a few underlying types from the C# lib.
One piece of code needs to calculate a list of values and assign it to a public field/property in the class. The field is a C# class that contains two ICollection.
My F# code works fine and needs to return an F# Seq/List.
I tried the following code snippets which each produce errors.
Return type of F# member is a type called recoveryList with type Recoveries list
Public field in class that is a class itself containing two ICollection objects
this.field.Collection1 = recoveries
This gives the error Expected to have type ICollection but has type Recoveries list
this.field.Collection1 = new ResizeArray<Recoveries>()
Gives the error expected type ICollection but is ResizeArray
this.field.Collection1 = new System.Collections.Generic.List<Recoveries>()
Same error as above - expected ICollection but type is List
Any ideas? These operations seem valid from a C# point of view and List/ResizeArray implements ICollection so... I am confused how to assign the value.
I could change the type of the underlying C# library, but this might have other implications.
Thanks
F# doesn't do implicit casting like C#. So even though System.Collections.Generic.List<'T> implements the ICollection interface, you can't directly set some ICollection-typed property to an instance of System.Collections.Generic.List<'T>.
The fix is easy though -- all you need to do is add an explicit upcast to ICollection to your ResizeArray<'T> or System.Collections.Generic.List<'T> before assigning it:
// Make sure to add an 'open' declaration for System.Collections.Generic
this.field.Collection1 = (recoveries :> ICollection)
or
this.field.Collection1 = (ResizeArray<Recoveries>() :> ICollection)

Get description of types in F# Interactive?

Can I get FSI.exe, the F# Interactive tool, to describe a type for me? In other words, there is a type I know how to access (specifically, IExpression in the Infer.NET library) but I do not know which methods it provides. I am hoping that I can use fsi to get a description of the available methods, properties, etc.
Can this be done, or is there a better way to go about it? I, sadly, do not have Visual Studio, which is how I used to get this information...
Thanks,
Nels
This depends on what you mean by "describe", and how automated a process you are looking for. It's quite easy to use the .NET reflection libraries to determine what public methods a type has. For example:
typeof<System.String>.GetMethods()
will give you an array containing MethodInfos for all of the public methods on the String class. You can do exactly the same thing for any other types.
You can write a simple method to streamline the process:
let showMethods(t:System.Type) =
t.GetMethods() |> Seq.iter (printfn "%A")

Resources