Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline Option module #14927

Merged
merged 6 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions src/FSharp.Core/option.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,55 +26,55 @@ module Option =
| Some _ -> false

[<CompiledName("DefaultValue")>]
let defaultValue value option =
let inline defaultValue value option =
match option with
| None -> value
| Some v -> v

[<CompiledName("DefaultWith")>]
let defaultWith defThunk option =
let inline defaultWith ([<InlineIfLambda>] defThunk) option =
match option with
| None -> defThunk ()
| Some v -> v

[<CompiledName("OrElse")>]
let orElse ifNone option =
let inline orElse ifNone option =
match option with
| None -> ifNone
| Some _ -> option

[<CompiledName("OrElseWith")>]
let orElseWith ifNoneThunk option =
let inline orElseWith ([<InlineIfLambda>] ifNoneThunk) option =
match option with
| None -> ifNoneThunk ()
| Some _ -> option

[<CompiledName("Count")>]
let count option =
let inline count option =
match option with
| None -> 0
| Some _ -> 1

[<CompiledName("Fold")>]
let fold<'T, 'State> folder (state: 'State) (option: 'T option) =
let inline fold<'T, 'State> ([<InlineIfLambda>] folder) (state: 'State) (option: 'T option) =
match option with
| None -> state
| Some x -> folder state x

[<CompiledName("FoldBack")>]
let foldBack<'T, 'State> folder (option: option<'T>) (state: 'State) =
let inline foldBack<'T, 'State> ([<InlineIfLambda>] folder) (option: option<'T>) (state: 'State) =
match option with
| None -> state
| Some x -> folder x state

[<CompiledName("Exists")>]
let exists predicate option =
let inline exists ([<InlineIfLambda>] predicate) option =
match option with
| None -> false
| Some x -> predicate x

[<CompiledName("ForAll")>]
let forall predicate option =
let inline forall ([<InlineIfLambda>] predicate) option =
match option with
| None -> true
| Some x -> predicate x
Expand All @@ -86,80 +86,80 @@ module Option =
| Some v -> v = value

[<CompiledName("Iterate")>]
let iter action option =
let inline iter ([<InlineIfLambda>] action) option =
match option with
| None -> ()
| Some x -> action x

[<CompiledName("Map")>]
let map mapping option =
let inline map ([<InlineIfLambda>] mapping) option =
match option with
| None -> None
| Some x -> Some(mapping x)

[<CompiledName("Map2")>]
let map2 mapping option1 option2 =
let inline map2 ([<InlineIfLambda>] mapping) option1 option2 =
match option1, option2 with
| Some x, Some y -> Some(mapping x y)
| _ -> None

[<CompiledName("Map3")>]
let map3 mapping option1 option2 option3 =
let inline map3 ([<InlineIfLambda>] mapping) option1 option2 option3 =
match option1, option2, option3 with
| Some x, Some y, Some z -> Some(mapping x y z)
| _ -> None

[<CompiledName("Bind")>]
let bind binder option =
let inline bind ([<InlineIfLambda>] binder) option =
match option with
| None -> None
| Some x -> binder x

[<CompiledName("Flatten")>]
let flatten option =
let inline flatten option =
match option with
| None -> None
| Some x -> x

[<CompiledName("Filter")>]
let filter predicate option =
let inline filter ([<InlineIfLambda>] predicate) option =
match option with
| None -> None
| Some x -> if predicate x then Some x else None

[<CompiledName("ToArray")>]
let toArray option =
let inline toArray option =
match option with
| None -> [||]
| Some x -> [| x |]

[<CompiledName("ToList")>]
let toList option =
let inline toList option =
match option with
| None -> []
| Some x -> [ x ]

[<CompiledName("ToNullable")>]
let toNullable option =
let inline toNullable option =
match option with
| None -> System.Nullable()
| Some v -> System.Nullable(v)

[<CompiledName("OfNullable")>]
let ofNullable (value: System.Nullable<'T>) =
let inline ofNullable (value: System.Nullable<'T>) =
if value.HasValue then
Some value.Value
else
None

[<CompiledName("OfObj")>]
let ofObj value =
let inline ofObj value =
match value with
| null -> None
| _ -> Some value

[<CompiledName("ToObj")>]
let toObj value =
let inline toObj value =
match value with
| None -> null
| Some x -> x
Expand Down
44 changes: 22 additions & 22 deletions src/FSharp.Core/option.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("DefaultValue")>]
val defaultValue: value: 'T -> option: 'T option -> 'T
val inline defaultValue: value: 'T -> option: 'T option -> 'T

/// <summary>Gets the value of the option if the option is <c>Some</c>, otherwise evaluates <paramref name="defThunk"/> and returns the result.</summary>
///
Expand All @@ -73,7 +73,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("DefaultWith")>]
val defaultWith: defThunk: (unit -> 'T) -> option: 'T option -> 'T
val inline defaultWith: defThunk: (unit -> 'T) -> option: 'T option -> 'T

/// <summary>Returns <paramref name="option"/> if it is <c>Some</c>, otherwise returns <paramref name="ifNone"/>.</summary>
///
Expand All @@ -91,7 +91,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("OrElse")>]
val orElse: ifNone: 'T option -> option: 'T option -> 'T option
val inline orElse: ifNone: 'T option -> option: 'T option -> 'T option

/// <summary>Returns <paramref name="option"/> if it is <c>Some</c>, otherwise evaluates <paramref name="ifNoneThunk"/> and returns the result.</summary>
///
Expand All @@ -110,7 +110,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("OrElseWith")>]
val orElseWith: ifNoneThunk: (unit -> 'T option) -> option: 'T option -> 'T option
val inline orElseWith: ifNoneThunk: (unit -> 'T option) -> option: 'T option -> 'T option

/// <summary>Gets the value associated with the option.</summary>
///
Expand Down Expand Up @@ -142,7 +142,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("Count")>]
val count: option: 'T option -> int
val inline count: option: 'T option -> int

/// <summary><c>fold f s inp</c> evaluates to <c>match inp with None -> s | Some x -> f s x</c>.</summary>
///
Expand All @@ -161,7 +161,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("Fold")>]
val fold<'T, 'State> : folder: ('State -> 'T -> 'State) -> state: 'State -> option: 'T option -> 'State
val inline fold<'T, 'State> : folder: ('State -> 'T -> 'State) -> state: 'State -> option: 'T option -> 'State

/// <summary><c>fold f inp s</c> evaluates to <c>match inp with None -> s | Some x -> f x s</c>.</summary>
///
Expand All @@ -180,7 +180,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("FoldBack")>]
val foldBack<'T, 'State> : folder: ('T -> 'State -> 'State) -> option: 'T option -> state: 'State -> 'State
val inline foldBack<'T, 'State> : folder: ('T -> 'State -> 'State) -> option: 'T option -> state: 'State -> 'State

/// <summary><c>exists p inp</c> evaluates to <c>match inp with None -> false | Some x -> p x</c>.</summary>
///
Expand All @@ -198,7 +198,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("Exists")>]
val exists: predicate: ('T -> bool) -> option: 'T option -> bool
val inline exists: predicate: ('T -> bool) -> option: 'T option -> bool

/// <summary><c>forall p inp</c> evaluates to <c>match inp with None -> true | Some x -> p x</c>.</summary>
///
Expand All @@ -216,7 +216,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("ForAll")>]
val forall: predicate: ('T -> bool) -> option: 'T option -> bool
val inline forall: predicate: ('T -> bool) -> option: 'T option -> bool

/// <summary>Evaluates to true if <paramref name="option"/> is <c>Some</c> and its value is equal to <paramref name="value"/>.</summary>
///
Expand Down Expand Up @@ -247,7 +247,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("Iterate")>]
val iter: action: ('T -> unit) -> option: 'T option -> unit
val inline iter: action: ('T -> unit) -> option: 'T option -> unit

/// <summary><c>map f inp</c> evaluates to <c>match inp with None -> None | Some x -> Some (f x)</c>.</summary>
///
Expand All @@ -263,7 +263,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("Map")>]
val map: mapping: ('T -> 'U) -> option: 'T option -> 'U option
val inline map: mapping: ('T -> 'U) -> option: 'T option -> 'U option

/// <summary><c>map f option1 option2</c> evaluates to <c>match option1, option2 with Some x, Some y -> Some (f x y) | _ -> None</c>.</summary>
///
Expand All @@ -282,7 +282,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("Map2")>]
val map2: mapping: ('T1 -> 'T2 -> 'U) -> option1: 'T1 option -> option2: 'T2 option -> 'U option
val inline map2: mapping: ('T1 -> 'T2 -> 'U) -> option1: 'T1 option -> option2: 'T2 option -> 'U option

/// <summary><c>map f option1 option2 option3</c> evaluates to <c>match option1, option2, option3 with Some x, Some y, Some z -> Some (f x y z) | _ -> None</c>.</summary>
///
Expand All @@ -303,7 +303,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("Map3")>]
val map3:
val inline map3:
mapping: ('T1 -> 'T2 -> 'T3 -> 'U) ->
option1: 'T1 option ->
option2: 'T2 option ->
Expand All @@ -330,7 +330,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("Bind")>]
val bind: binder: ('T -> 'U option) -> option: 'T option -> 'U option
val inline bind: binder: ('T -> 'U option) -> option: 'T option -> 'U option

/// <summary><c>flatten inp</c> evaluates to <c>match inp with None -> None | Some x -> x</c></summary>
///
Expand All @@ -348,7 +348,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("Flatten")>]
val flatten: option: 'T option option -> 'T option
val inline flatten: option: 'T option option -> 'T option

/// <summary><c>filter f inp</c> evaluates to <c>match inp with None -> None | Some x -> if f x then Some x else None</c>.</summary>
///
Expand All @@ -365,7 +365,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("Filter")>]
val filter: predicate: ('T -> bool) -> option: 'T option -> 'T option
val inline filter: predicate: ('T -> bool) -> option: 'T option -> 'T option

/// <summary>Convert the option to an array of length 0 or 1.</summary>
///
Expand All @@ -380,7 +380,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("ToArray")>]
val toArray: option: 'T option -> 'T[]
val inline toArray: option: 'T option -> 'T[]

/// <summary>Convert the option to a list of length 0 or 1.</summary>
///
Expand All @@ -395,7 +395,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("ToList")>]
val toList: option: 'T option -> 'T list
val inline toList: option: 'T option -> 'T list

/// <summary>Convert the option to a Nullable value.</summary>
///
Expand All @@ -410,7 +410,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("ToNullable")>]
val toNullable: option: 'T option -> Nullable<'T>
val inline toNullable: option: 'T option -> Nullable<'T>

/// <summary>Convert a Nullable value to an option.</summary>
///
Expand All @@ -425,7 +425,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("OfNullable")>]
val ofNullable: value: Nullable<'T> -> 'T option
val inline ofNullable: value: Nullable<'T> -> 'T option

/// <summary>Convert a potentially null value to an option.</summary>
///
Expand All @@ -440,7 +440,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("OfObj")>]
val ofObj: value: 'T -> 'T option when 'T: null
val inline ofObj: value: 'T -> 'T option when 'T: null

/// <summary>Convert an option to a potentially null value.</summary>
///
Expand All @@ -455,7 +455,7 @@ module Option =
/// </code>
/// </example>
[<CompiledName("ToObj")>]
val toObj: value: 'T option -> 'T when 'T: null
val inline toObj: value: 'T option -> 'T when 'T: null

/// <summary>Contains operations for working with value options.</summary>
///
Expand Down
2 changes: 1 addition & 1 deletion tests/projects/SelfContained_Trimming_Test/check.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if (-not ($output -eq $expected))
}

# Checking that FSharp.Core binary is of expected size (needs adjustments if test is updated).
$expected_len = 249856 # In bytes
$expected_len = 249344 # In bytes
$file = Get-Item .\bin\Release\net7.0\win-x64\publish\FSharp.Core.dll
$file_len = $file.Length
if (-not ($file_len -eq $expected_len))
Expand Down