fsharp / fsharp/fslang-suggestions

Compiler switch to make struct tuples the default

Open
#813 19 comments 8 reactions 0 assignees View on GitHub
area: optimization
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I propose we introduce a compiler switch to change the default inference of tuples as value tuples where InferAsValueTuples = X will cause all tuples with length less than X being inferred as ValueTuples.
The default value of X will be 0 and will effectively turn off this feature.

The existing way of approaching this problem in F# is to use struct(x,y) but many times the users don't use the struct keyword and this creates the following problems
- It requires more typing.
- It creates a false sense of correctness as in ref tuples are better defaults since arguably especially for smaller tuples, ValueTuples perform better and are better defaults.

C# people are already using ValueTuples by default.

## Pros and Cons

The advantages of making this adjustment to F# are
- People will use saner default as ValueTuples
- Less typing and easier reading
- Better C# ValueTuple interop

- Gradually increase the default value of X if needed.

The disadvantages of making this adjustment to F# are
- It may cause some confusion when different projects use different values for X but source will still compile no matter what.
- At source code level it may break things as well as at binary level.
- The author of the code has to remember what X is, but still he will get a meaningful error message if something goes wrong.
- If you really need a ref tuple in all cases you must give the type hint explicitly.
`let (x,y):(int*int) = 3,5`

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

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue does not identify implementation files, compiler entry points, or tests. First review the existing tuple inference and struct tuple design, then determine how an InferAsValueTuples setting would affect type inference, source compatibility, and binary compatibility; done requires an agreed language-design decision and corresponding implementation and tests.

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
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.