You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I propose we introduce flow based null check analysis for values of a type that marked as [<AllowNullLiteralAttribute>] and any typical .NET reference type (not defined in F#)
The existing way of approaching this problem in F# is to do explicit check
lets:string = CSharpClass().GetName()if(s !=null)
printfn "%i" s.Length
else
failwith "What do I do with NULL???"
Easy to forget checks lead to NullReferenceException.
It would make code more robust if F# compiler can enforce this checks.
e.g. following code won't compile complaining that s.Length can result in NRE without null checking
In F# nulls are slightly smaller issue that in other languages because native F# types cannot have null as normal value. Therefore this extra strict check should be opt-in. Thera are several ways to trigger verification:
Introduce "--strictNullChecks" compiler switch.
It means all code in a project should pass this check. I don't think it's practical to have only this option because all of sudden your whole code base doesn't compile and there are dozens if not hundred places where it has to be fixed. But somebody building mission-critical, super robust component might want to turn on this switch.
More fine-grained approach is to have special attribute on function or method
It will force checks inside a body of marked function or method including input parameters.
This attribute should not be inherit-able.
It's possible that [<StrictNullChecks>] can be applied to a method parameter only but it seems not much gain over doing simple check or using Option<_> type.
The attribute can be applied on module or type level too.
Also would be nice if null check verification will flow within F# code base. e.g.
moduleAssert [<StrictNullChecks>]letnotNull x =...[<StrictNullChecks>]letfoo()=...lets= CSharpClass().GetName()
Assert.notNull s
//safe to access properties like Length
printfn "%i" s.Length
...
Pros and Cons
The advantages of making this adjustment to F# are: the code will be even more null-safe
The disadvantages of making this adjustment to F# are ... a lot of language design and compiler work
Extra informtion
Estimated cost (XS, S, M, L, XL, XXL): XL
Related suggestions: (put links to reated suggestions here)
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
This is not a breaking change to the F# language design
I would be willing to help implement and/or test this
I or my company would be willing to help crowdfund F# Software Foundation members to work on this
The text was updated successfully, but these errors were encountered:
@smoothdeveloper This proposal is mostly to enforce strict checks on reference values coming out of non-F# APIs.
There is no need to add ! operator because F# is already great at null-tracking.
If it's F# API use Option<_> type like
letdoWithNonNull(text:string option)=...
If it's a value that is going to be passed to non-F# API use Option<_> to do all computations and convert it to a ref via Option.toObj
letx:string option =...//compute...
x |> Option.toObj |> doWithNonNull
Title of Suggestion
I propose we introduce flow based null check analysis for values of a type that marked as
[<AllowNullLiteralAttribute>]
and any typical .NET reference type (not defined in F#)The existing way of approaching this problem in F# is to do explicit check
Easy to forget checks lead to NullReferenceException.
It would make code more robust if F# compiler can enforce this checks.
e.g. following code won't compile complaining that
s.Length
can result in NRE without null checkingThis is similar to how Typescript or Kotlin compilers do null-safety analysis.
microsoft/TypeScript#8010
microsoft/TypeScript#7140
https://kotlinlang.org/docs/reference/null-safety.html
In F# nulls are slightly smaller issue that in other languages because native F# types cannot have null as normal value. Therefore this extra strict check should be opt-in. Thera are several ways to trigger verification:
Introduce "--strictNullChecks" compiler switch.
It means all code in a project should pass this check. I don't think it's practical to have only this option because all of sudden your whole code base doesn't compile and there are dozens if not hundred places where it has to be fixed. But somebody building mission-critical, super robust component might want to turn on this switch.
More fine-grained approach is to have special attribute on function or method
It will force checks inside a body of marked function or method including input parameters.
This attribute should not be inherit-able.
It's possible that
[<StrictNullChecks>]
can be applied to a method parameter only but it seems not much gain over doing simple check or usingOption<_>
type.The attribute can be applied on module or type level too.
Also would be nice if null check verification will flow within F# code base. e.g.
Pros and Cons
The advantages of making this adjustment to F# are: the code will be even more null-safe
The disadvantages of making this adjustment to F# are ... a lot of language design and compiler work
Extra informtion
Estimated cost (XS, S, M, L, XL, XXL): XL
Related suggestions: (put links to reated suggestions here)
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: