hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

How to access F# Option type from C#

Last post 03-22-2008, 16:01 by nmpgaspar. 4 replies.
Sort Posts: Previous Next
  •  03-04-2008, 10:12 5186

    How to access F# Option type from C#

    Hi,

    I'm fairly new to F# and having to work in both F# and C# in the same project. I need to access a variable declared in F# with type "Type [] option" from C#. Using reflector I established that the .NET type is "Option<T> with T = Type[]. This type has a property called Value for retrieving the value when it is present, which looks good to use. However, I also need to enquire whether the value is present. The type also has Boolean properties called IsNone and IsSome, however they are static properties, not member properties, and they appear to need an argument of type Option<!T> which I think means Option of non-nullable T - and not compatible with Option<T>.

    Is there a reason why these properties are not simple member properties? Can anyone suggest how I can query from C# whether the variable has a value set or not?

    Thanks in advance!

  •  03-05-2008, 0:47 5192 in reply to 5186

    Re: How to access F# Option type from C#

    The option type, along with other native F# types such as tuples, are defined in the FSharp.core.dll library (called fslib.dll in older versions of F#). To use these types from C# you just need to add a refernce to this library. Union types like option look a little strange to C# but are still usable.

    Cheers,
    Rob


    Robert Pickering, MVP
    http://strangelights.com
  •  03-21-2008, 14:40 5447 in reply to 5192

    Re: How to access F# Option type from C#

    I've already added the fslib.dll library, which removed me an error but i'm still not able to do wath i want.

    fsharpFile.fs

    let returnAList (number:int) = [number;number;number];;

    csharpFile.cs

    List<int> myList = ProjectFsharp.fsharpFile.returnAList(5);

    but that line on C# file gives me: "Cannot implicitly convert type 'Microsoft.Fsharp.Collections.List<int>' to 'system.Collections.Generic.List<int>'"

    Even if i explicitly put the cast it gives an error. any way to fix this?

    Also, if i wanted to receive a tuple list from F#, how would i receive it in C#? Should i had to make an object with the same types of the tuple elements of the list? or ? Little example please? :)

     

     

     

  •  03-21-2008, 19:04 5449 in reply to 5447

    Re: How to access F# Option type from C#

    A C# list (System.Collections.Generic.List) and an F# list (Microsoft.FSharp.Collections.List) are completely different types. A C# list has a constructor that accepts an IEnumerable to initialize the list, and an F# list implements IEnumerable, so you could write

    List<int> myList = new List<int>(ProjectFsharp.fsharpFile.returnAList(5));

    F# tuples are represented in C# as the types Microsoft.FSharp.Core.Tuple, with varying number of type parameters depending on the size of the tuple. So you could write

    using System.Collections.Generic;
    using Microsoft.FSharp.Core;

    List<Tuple<int,int>> myListOfIntIntTuples;

  •  03-22-2008, 16:01 5470 in reply to 5449

    Re: How to access F# Option type from C#

    Working :)

    Thanks mate!

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems