Using F # Option Type in C #

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 recursive "list information" is an option. Either there or not. I need to use this from C #, but I get errors. This is a usage example:

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

here listInfo is of type ListInfo as above. I'm just trying to check if it contains a value, and if so, then I want to use it. But all calls to listInfo.LInfo give the error "The property, index or event of listInfo.LInfo is not supported by the language ..."

Who understands why?

+7
source share
1 answer

I suspect the problem is that the getter / setter property of the LInfo property works with different types (which is not supported in C #).

try it

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

Or that

 member this.LInfo with get() = match lInfo with Some x -> x | None -> Unchecked.defaultof<_> and set value = lInfo <- Some value 
+3
source

All Articles