kasperhj
kasperhj

Reputation: 10482

Declaring a generic variable in a type in F#

How do I declare that I want something of type 'a which I have no value for yet.

type MyType<'a> =
    let mutable something:'a = ??
    let setSomething item:'a =
        something <- a

Upvotes: 1

Views: 136

Answers (1)

scrwtp
scrwtp

Reputation: 13577

You can use Unchecked.defaultof<'a>. That said, a nicer way for me would be to make something an Option of 'a. That way the intention of ”not having a value“ would be made clearer.

type MyType<'a> =
    let mutable something:'a option = None
    let setSomething (item:'a) =
        something <- Some a

Upvotes: 6

Related Questions