Just wanted to show you the code I think you intended to write:

1
2
3
4
5
6
7
8
9
10
type MyClass(id : int, ip : string) = 

    let currPosition : string option ref = ref None

    member v.CurrPosition 

        with get() = !currPosition

        and  set(value) = currPosition := value

(I replaced PositionMsg by string)

Notice that ref is the outer type, so that you can update it, and notice the right syntax to assign a value to a ref type (:=, NOT =, that means compare for equality) and the right syntax to read a value from a ref type (!).

That should at least also remove the need to deal with the ref type from C#.

A maybe simpler alternative is:

1
2
3
4
5
6
7
8
9
10
type MyClass2(id : int, ip : string) = 

    let mutable currPosition : string option = None

    member v.CurrPosition 

        with get() = currPosition

        and  set(value) = currPosition <- value

Notice again the different syntax for assignment ( <- ).

Kurt

By on 3/12/2009 1:42 PM ()

The short answer is "don't do that". F#-specific types like list, option, and ref do not look pretty from other languages, and you should ideally avoid using them at public interface boundaries.

That said, there's "Option.is_some" and "Option.get" to test and retrieve the value:

[link:research.microsoft.com]

but you'd have to reference FSharp.Core.dll from the C# project to use it. (And once you do, you'd still need to deal with 'ref' in your example.)

By on 3/12/2009 1:20 PM ()

Brian,

what would be the best way to deal with this situation. I have 2 F# classes - MyClass have reference to PositionMsg which is not mandatory.

I want to use them from C# and I need to test if PositionMsg is assigned and if it is, then C# code should read some values from that class. Is there another way for creating non mandatory property in F# that is more "C# friendly"?

By on 3/13/2009 12:07 AM ()

You could return 'null' if unassigned, if that's a reasonable semantic from C# perspective. Note that if PositionMsg is defined in F#, you'll have to use Unchecked.defaultof to create a null value of that type.

Forget F# for a moment. If this were C#, what API would you choose? Then go implement that.

By on 3/13/2009 12:54 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper