dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.82k stars 773 forks source link

Feature nullness :: Try infer without null even when function/method arg is marked as nullable #17269

Closed T-Gro closed 3 weeks ago

T-Gro commented 1 month ago

This is motivated by common usage of "null-allowing" parameters within the BCL. In this example, "File.Exists" allows a nullable string as input.

let myFunction path
  let exists = File.Exists path
  ...
  path.ToString()    // <- warning used to be here

In the past, this would have caused the inferred function parameter to also become a nullable string, and therefore subsequent code would fail if it needed a string without nulls.

This did not seem "fair" to users of safe F# without overuse of nulls - why should they spend any effort to annotate non-nullable parameters, when this should be the default.

This PR inverts this, and it parameters that need to be nullable will have to be explicitely annotated, not vice versa. This mean that even when a parameter is used across null-allowing calls, it would be inferred as withoutNull by default.

This does create some additional work for the case of simple wrappers with the desire to keep them null-allowing. But I believe this should be a less common pattern for F# code, compared to not wanting to deal with nulls. The change in the positive.fs file shows the downside this PR creates.