[ACCEPTED]-F# keyword 'Some'-keyword

Accepted answer
Score: 51

Some is not a keyword. There is an option type however, which 5 is a discriminated union containing two 4 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 3 a nullable type, where you want to have 2 an object which can hold a value of some 1 type or have no value at all.

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

Can check out Discriminated Unions in F# for more info on DUs in general 5 and the option type (Some, None) in particular. As 4 a previous answer says, Some is just a union-case 3 of the option<'a> type, which is a 2 particularly common/useful example of an 1 algebraic data type.

Score: 7

Some is used to specify an option type, or in 10 other words, a type that may or may not 9 exist.

F# is different from most languages 8 in that control flow is mostly done through 7 pattern matching as opposed to traditional 6 if/else logic.

In traditional if/else logic, you 5 may see something like this:

if (isNull(x)) {
   do ...  
} else {         //x exists
   do ...  
}

With pattern 4 matching logic, matching we need a similar 3 way to execute certain code if a value is 2 null, or in F# syntax, None

Thus we would have 1 the same code as

match x with 
  | None -> do ...
  | Some x -> do ... 

More Related questions