-
Notifications
You must be signed in to change notification settings - Fork 21
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
Simplified parameter null validation code #1014
Comments
Open questions:
|
Similarly to #883 , this needs to be considered in the context of https://github.com/fsharp/fslang-design/blob/master/RFCs/FS-1060-nullable-reference-types.md . Then your example becomes: let length (array: 'a[]) =
// don't need a null check here since the function conveys that it expects a non-null argument
array.Length
// or
let length (array: ('a[])?) =
match array with
| Null -> nullArg ...
| NonNull a -> a.Length |
@charlesroddie Not really. Like in C# with nullable reference types, the argument itself is nonnullable but it defends against users with nullable reference type analysis disabled. |
It would be nice if ActivePatterns could use this too. Ideally : let (|NotNull|) (obj, [<Optional; DefaultParameterValue(null); CallerArgumentExpression("argument")>] description) = ArgumentNullException.ThrowIfNull(obj, description); obj
let nullCheckingCode (NotNull parameter: string) = printfn $"{parameter}" For now it doesn't work. It would be fun if ActivePatterns could support optional parameters like these. let (|NotNull|) description x = ArgumentNullException.ThrowIfNull(x, description); x
let nullCheckingCode (NotNull "parameter" parameter: string) (NotNull "parameter2" parameter2) = printfn $"{parameter + parameter2}" Or without description. |
@dsyme Do you think it's possible or reasonable to do with active patterns? I think it's a completely different proposal (like supporting optional parameters in active patterns) but it would be nice if you could take a small look at example above and say if you like the idea at all |
I'm not really sold on doing either of these in FSharp.Core. I don't really mind how people program it up in user code assuming CallerArgumentExpression is avaialble. |
Closing per my comment above |
I propose we add a new function to simplify parameter null validation code.
That's it!
Example usage:
Here will be the secret sauce in FSharp.Core:
This will work by building on CallerArgumentExpression.
The existing way of approaching this problem in F# is defining this function yourself or writing the long code every time:
Compared to the C# equivalent of this feature, this does not introduce new syntax (
!
), is much easier to understand (!
after the argument name itself?), and also makes the intention really explicit without the!
being too easy to miss.Pros and Cons
The advantages of making this adjustment to F# are
The disadvantage of making this adjustment to F# is that this is one more way to do the same thing, but is considerably simpler.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): XS
Related suggestions: CallerArgumentExpression
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: