Skip to content

Commit 5b8157f

Browse files
authored
Add conditionals for F#9 null handling (#665)
1 parent a931844 commit 5b8157f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/FSharpPlus/Extensions/Obj.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ module Obj =
99
/// <param name="source">The source object.</param>
1010
/// <returns>The source if it is not null; otherwise, the specified default value.</returns>
1111
/// <remarks>The default value is evaluated eagerly.</remarks>
12+
#if NET9_0_OR_GREATER && !FABLE_COMPILER
13+
let defaultValue value (source: 'T | null) : 'T =
14+
#else
1215
let defaultValue value (source: 'T) : 'T =
16+
#endif
1317
match source with
1418
| null -> value
1519
| value -> value
@@ -19,7 +23,11 @@ module Obj =
1923
/// <param name="source">The source object.</param>
2024
/// <returns>The source if it is not null; otherwise, the result of invoking the specified function.</returns>
2125
/// <remarks>The fNull function is only invoked if the source is null.</remarks>
26+
#if NET9_0_OR_GREATER && !FABLE_COMPILER
27+
let inline defaultWith ([<InlineIfLambda>]fNull: unit -> 'T) (source: 'T | null) : 'T =
28+
#else
2229
let inline defaultWith ([<InlineIfLambda>]fNull: unit -> 'T) (source: 'T) : 'T =
30+
#endif
2331
match source with
2432
| null -> fNull ()
2533
| value -> value
@@ -30,7 +38,11 @@ module Obj =
3038
/// <param name="source">The source object.</param>
3139
/// <returns>The result of applying fValue to the source if it is not null; otherwise, the result of applying fNull.</returns>
3240
/// <remarks>Only one of the functions is invoked based on the nullity of the source.</remarks>
41+
#if NET9_0_OR_GREATER && !FABLE_COMPILER
42+
let inline either ([<InlineIfLambda>]fValue: 'T -> 'U) ([<InlineIfLambda>]fNull: unit -> 'U) (source: 'T | null) : 'U =
43+
#else
3344
let inline either ([<InlineIfLambda>]fValue: 'T -> 'U) ([<InlineIfLambda>]fNull: unit -> 'U) (source: 'T) : 'U =
45+
#endif
3446
match source with
3547
| null -> fNull ()
3648
| value -> fValue value

src/FSharpPlus/Internals.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ module Errors =
4949
let exnNoSubtraction = new System.Exception "No subtraction defined for these values in this domain."
5050
let exnUnreachable = new System.InvalidOperationException "This execution path is unreachable."
5151

52-
// Functions to remove when compiling with F#9 or higher
52+
#if !NET9_0_OR_GREATER || FABLE_COMPILER
5353
let inline nullArgCheck paramName paramValue =
5454
if isNull paramValue then nullArg paramName
5555
else paramValue
5656

5757
module Unchecked = let nonNull = id
58+
#endif
5859

5960
module Decimal =
6061
let inline trySqrt x =

0 commit comments

Comments
 (0)