FSharpPlus
FSharpPlus copied to clipboard
Null handling in F#+
When 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#+
We should, of course, where it makes sense, like in cases like that one.
Now that we have nameof
available it should be easier to manage checks like this, although I'm not sure if/when CIs will support F#5
I can do an analysis and introduce null handling similar to FSharp.Core.
It would be done the same way as in Core, by passing explicit string. After moving to F#5 in F#+ we can change it to the nameof
version.
What do you think @gusty?
I think that's great. If you are happy to do that, just go ahead.
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.