Null handling in F#+
See original GitHub issueWhen searching through FSharp.Core code, I’ve found that it handles null cases in its external APIs. In F#+ we don’t handle nulls at all, which results in inconsistencies with core library.
Example situation:
// We have two arrays and one is null
let a1: int [] = null
let a2: int [] = [|1;2;3|]
// This fails elegantly with
// System.ArgumentNullException: Value cannot be null. (Parameter 'array1')
// at Microsoft.FSharp.Collections.ArrayModule.Map2[T1,T2,TResult](FSharpFunc`2 mapping, T1[] array1, T2[] array2)
Array.map2 (+) a1 a2
// This fails with null ref in first line of implementation usage
// System.NullReferenceException: Object reference not set to an instance of an object.
// at FSharpPlus.Array.lift2[a,b,c](FSharpFunc`2 f, a[] x, b[] y) in C:\Repos\FSharpPlus\src\FSharpPlus\Extensions\Array.fs:line 26
Arrayl.lift2 (+) a1 a2
FSharp.Core implementation goes like
let map2 mapping (array1: 'T[]) (array2: 'U[]) =
checkNonNull "array1" array1
checkNonNull "array2" array2
// ...
I wonder if we should do similar checks in F#+
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Handling Null Values in F# - Stack Overflow
Null is a possible value that it is given so I need to check if the value was null. The docs suggest using...
Read more >Null Values - F# | Microsoft Learn
The null keyword is a valid keyword in F#, and you have to use it when you are working with .NET Framework APIs...
Read more >Two Ways To Handle [NULL] Values
Null values are classed as cells with missing values and getting rid of them is one of the most important steps to ensure...
Read more >Adventures in null handling: Null money, null problems
Null can work as expected in one language and context, then act in completely unexpected ways in another language or context.
Read more >How to handle nulls in your Workato formula
Here are 3 tips that you can use to avoid formula errors when working with nulls in your Workato recipe: Prefixes, Alternate Values,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m experimenting with compiling F#+ with .net 5 in #393 , travis and github actions seem to support it.
Reviving this issue. In private conversations with @abelbraaksma the subject came again this time re string functions.
It turns out that in FSharp.Core the null handling for String functions is completely different and I would say completely opposite to what they do with say Arrays.
To avoid repeating myself, here’s a discussion about it https://github.com/dotnet/fsharp/discussions/14005 I started in a broader context.
So, I’m still holding adding some
null
checks, until we agree on a standard treatment, in order to not surprise users.