F# keyword 'Some'

前端 未结 3 595
闹比i
闹比i 2021-02-06 21:24

F# keyword \'Some\' - what does it mean?

3条回答
  •  余生分开走
    2021-02-06 21:56

    Some is not a keyword. There is an option type however, which is a discriminated union containing two things:

    1. Some which holds a value of some type.
    2. None which represents lack of value.

    It's defined as:

    type 'a option =
        | None
        | Some of 'a
    

    It acts kind of like a nullable type, where you want to have an object which can hold a value of some type or have no value at all.

    let stringRepresentationOfSomeObject (x : 'a option) =
        match x with
        | None -> "NONE!"
        | Some(t) -> t.ToString()
    

提交回复
热议问题