3

Can anyone help me with example on when an explicit field is necessary in F#? for instance, how would the following three classes be all useful

type MyClass =
    val a : int
    val b : int
    new(a0, b0) = { a = a0; b = b0; }

or

type MyClass() =
    [<DefaultValue>] val mutable a: int
    [<DefaultValue>] val mutable b: int
    member this.Setab( a0: int, b0: int) =
       a<- a0
       b<- b0

comparing with

type MyClass(a0:int,b0:int) =
    member x.a = a0
    member x.b = b0

I can only understand the last class. thanks.

EDIT: the following question is an example that the first two notions are necessary: Order of fields in a type for FileHelpers

1 Answer 1

5

The first two forms have some limited uses, they are useful if you need to be very explicity about the way the fields in a class are laid out. For example you maybe passing the type to unmanaged code which expects a certain number of fields in a certain or order, or you maybe passing it to an api that uses reflection over the fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.