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
let s: 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
let s = CSharpClass().GetName()
printfn "%i" s.Length
This 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
[<StrictNullChecks>]
let f() = ...
//or
type Foo() =
[<StrictNullChecks>]
member this.Bar() =
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.
module Assert
[<StrictNullChecks>]
let notNull x = ...
[<StrictNullChecks>]
let foo()= ...
let s = 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:
Please tick all that apply:
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.Lengthcan 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: