Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using F# Option Type in C#

Tags:

c#

option

f#

I have the following type:

and ListInfo() =

let mutable count = 0

// This is a mutable option because we can't have an infinite data structure.
let mutable lInfo : Option<ListInfo> = None

let dInfo = new DictInfo()
let bInfo = new BaseInfo()

member this.BaseInfo = bInfo
member this.DictInfo = dInfo

member this.LInfo
    with get() = lInfo
    and set(value) = lInfo <- Some(value)

member this.Count
    with get() = count
    and set(value) = count <- value

where the recursive "list info" is an Option. Either there is one or there is none. I need to use this from C# but I get errors. This is a sample usage:

if (FSharpOption<Types.ListInfo>.get_IsSome(listInfo.LInfo))
{
    Types.ListInfo subListInfo = listInfo.LInfo.Value;
    HandleListInfo(subListInfo, n);
}

here listInfo is of the type ListInfo as above. I'm just trying to check if it contains a value and if so I want to use it. But all the accesses listInfo.LInfo gives the error "Property, indexer or event listInfo.LInfo is not supported by the language..."

Anyone that understands why?

like image 664
UmaN Avatar asked Dec 21 '11 13:12

UmaN


People also ask

What is the use of \F?

The most common one is newline \n , but there are others. Many of them became less and less useful over time, such as \f . This is called form feed, is used to indicate to a printer that it should start a new page.

What is f {} in Python?

“F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with f , which contains expressions inside braces.

How do you use f-string?

To create an f-string, prefix the string with the letter “ f ”. The string itself can be formatted in much the same way that you would with str. format(). F-strings provide a concise and convenient way to embed python expressions inside string literals for formatting.

What is %d and %f in Python?

For example, "print %d" % (3.78) # This would output 3 num1 = 5 num2 = 10 "%d + %d is equal to %d" % (num1, num2, num1 + num2) # This would output # 5 + 10 is equal to 15. The %f formatter is used to input float values, or numbers with values after the decimal place.


1 Answers

I suspect the problem is the LInfo property getter/setter work with different types (which isn't supported in C#).

Try this

member this.LInfo
    with get() = lInfo
    and set value = lInfo <- value

Or this

member this.LInfo
    with get() = match lInfo with Some x -> x | None -> Unchecked.defaultof<_>
    and set value = lInfo <- Some value
like image 200
Daniel Avatar answered Sep 29 '22 13:09

Daniel