fsharp / fsharp/fslang-suggestions

Warn when a function is an interpolated string argument / when a type that isn't IFormattable gets format arguments

Open
#1,435 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

approved-in-principle area: diagnostics
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I propose we put a warning here -

(from https://github.com/dotnet/fsharp/issues/18749 @esbenbjerre)

open System

type DateTimeRange =
  {
    Start: DateTime
    End: DateTime
  }

  override _.ToString() = $"{_.Start:O} / {_.End:O}" // Warn on _.Start and _.End

let range =
  {
    Start = DateTime.UtcNow
    End = DateTime.UtcNow.AddSeconds 2.0
  }

$"{range}"

This code looks like it's using _ as a self-identifier when in fact the self-identifier is discarded and _.Start expands to fun x -> x.Start. Similar pitfalls exist when a partially-applied function is used as a string interpolation argument.

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

Pros and Cons

The advantage of making this adjustment to F# is robustness (less bugs)

The disadvantages of making this adjustment to F# is needing to #nowarn when you really want to get readable information about lambdas. I haven't encountered a need for this though.

Extra information

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

Related suggestion:

Affidavit (please submit!)

Please tick these items 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
  • This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository
  • 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
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate

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

For Readers

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the interpolated-string examples in the issue and review the related suggestion at fsharp/fslang-suggestions#1327. Determine the language-design scope for warnings involving discarded self-identifiers, partially applied functions, and non-IFormattable format arguments; done means the warning behavior and cases are specified clearly enough to implement and test.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.