fsharp / fsharp/fslang-suggestions

Deprecate Option<T> Implicit Casting for safer C# experience with FSharpOption<T>

Open
#1,048 2 comments 2 reactions 0 assignees View on GitHub
area: interop
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

# Title of Suggestion

I propose we mark `op_Implicit: T -> T option` as obsolete. This is related to the issue posted in [dotnet/fsharp #11565](https://github.com/dotnet/fsharp/issues/11565). From the first line in that issue:

When implicitly assigning a null variable of `T` to an `FSharpOption`, the value will be of `Some(null)` instead of `None` (which is `null`). However doing this with a `null` constant produces `None` as expected.

In theory this should create a better experience of using `FSharpOption` in C# because the C# sematics are rather ugly when using FSharpOption, but the major downside is that anyone unfamiliar (and possibly familiar) with this issue will forget to null check and set the value to None in C# before passing to F# and break the "Safe" F# core. There are further abstractions (such as writing the models in F# instead of C#, even with a C# API), but it makes it that much harder to recommend mixing F#/C# and using the Option type to get optimal code safety.

Code Snippet example, (There's another in the issue linked above if helpful):

```csharp
// F# Type
// type Test = {
// Text: string option
// }

string text = "b";
string nullText = null;
new Test("a").Text; // Some "a"
new Test(text).Text; // Some "b"
new Test(null).Text; // None
new Test(nullText).Text; // Some null
```

## Pros and Cons

The advantages of making this adjustment to F# are safer interoperability, as the compiler will now tell us (at least with a warning) not to do this because of what I personally think would be unwanted `Some(null)`

The disadvantages of making this adjustment to F# are that warnings can be easy to ignore, and it would still leave us with having to declare `FSharpOption.Some(value)` or `FSharpOption.None` in several parts of our C# code base (at least without the use of extension methods).

## Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

## Author's Note

I don't have any good ideas right now of what a better implementation would be. I hesitated a while before posting, but If I think of anything I'll be sure to come back and pitch an idea. At this point in my own code base I just wrote a couple of extension methods on the `object` type that does the conversion as I would expect, It would just be a lot nicer for the compiler to remind me and my team to use them :).

## Affidavit

Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] 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:
* [x] This is not a breaking change to the F# language design
* [x] I would be willing to help implement and/or test this

## For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the linked dotnet/fsharp #11565 issue and the current op_Implicit declaration. This issue names no repository files or tests, so first establish the compatibility impact of deprecation for existing C# callers and agree on the intended behavior. Done requires an accepted design and identified validation before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.