Skip to content

Diagnostics: warn if a with-operation updates all values #603

@0x53A

Description

@0x53A

Diagnostics: warn if a with-operation updates all values

I propose we ... warn if a with-operation updates all values.

Consider the following snippet:

type MyRecord = { A : int list ; B : string list }
let combine a b = { a with A = a.A @ b.A ; B = a.B @ b.B }

combine { A = [ 1 ] ; B = [ "2" ] }  { A = [ 1 ] ; B = [ "2" ] }

If I now update the type MyRecord and add a third field, the compiler will guide me to adapt the two constructors, but will NOT guide me to adapt combine.

type MyRecord = { A : int list ; B : string list ; C : char list }
let combine a b = { a with A = a.A @ b.A ; B = a.B @ b.B } // <- C is missing

combine { A = [ 1 ] ; B = [ "2" ] ; C = [] }  { A = [ 1 ] ; B = [ "2" ] ; C = [ '3' ] }

Using a with doesn't really make sense in this situation, because all values are updated anyway, but I have often seen this pattern in the wild! So I would want this warning to make later refactorings easier. Note that this warning would not be issued after adding the third field, because by that time, the with would no longer update all fields.

The existing way of approaching this problem in F# is ...

Pros and Cons

The advantages of making this adjustment to F# are ...

Makes it easier to find places to adapt after adding a record field.

The disadvantages of making this adjustment to F# are ...

Perfectly fine code may produce this diagnostic, but the fix is easy and will improve the code base.
Any additional warning may break code that currently compiles if warn-as-errors is enabled. I think it would be worth it.

Extra information

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

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

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
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • 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
  • Any additional warning may break code that currently compiles if warn-as-errors is enabled. I think it would be worth it.
  • I or my company would be willing to help implement and/or test this

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions