fsharp / fsharp/fslang-suggestions

Make it illegal to pass a temporary ref-cell as byref

Open
#686 3 comments 1 reaction 0 assignees View on GitHub
approved-in-principle area: structs-byrefs-and-span
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I propose we ... _Make it illegal to pass a temporary ref-cell as byref_

### related

(see https://github.com/Microsoft/visualfsharp/pull/5542)

With the F# 4.5 work, the following previously compiling code errors out:

```f#
type Incrementor(delta) =
member this.Increment(i : byref ) = i <- i + delta
let kvp = KeyValuePair(1, 2)
incrementor.Increment(&kvp.Value)
```

the reason is that this does __not__ take the address of ``.Value`` (which is a property), but instead copies it into a temporary local and takes the address of _that local_.

### suggestion

I suggest to do the same for the pattern

```f#
type Incrementor(delta) =
member this.Increment(i : byref ) = i <- i + delta
let i = 1
incrementor.Increment(ref i)
```

Everyone who isn't intimately familiar with ref-cells would expect the above code to mutate i. Instead it allocates a temporary ref-cell and takes the address of that.

It desugars to: ``incrementor.Increment(&({contents=x}.contents@))`` (where ``contents@`` is the backing field for the property)

Especially for people coming from C#, the difference between ``ref`` and ``&`` is confusing, doubly so because ``&`` is equivalent to the ``ref`` keyword in c#.

Note that the following code would still be legal, because ref-cells are just normal values which I can pass around:

```f#
let f (x: int ref) = () // this receives a ref-cell, not a byref
let i = 1
f (ref i)
```

## Pros and Cons

The advantages of making this adjustment to F# are ... 99.99% of occurrences of this pattern are bugs. I am tempted to say 100% but some people write horrible code.

The disadvantages of making this adjustment to F# are ... it's a breaking change

## Extra information

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

Related suggestions: (put links to related suggestions here)

## Affidavit (please submit!)

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:
* [ ] This is not a breaking change to the F# language design
* [ ] I or my company would be willing to help implement and/or test this

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.