[ACCEPTED]-F# keyword 'Some'-keyword
Some
is not a keyword. There is an option
type however, which 5 is a discriminated union containing two 4 things:
Some
which holds a value of some type.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()
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.
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
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.