fsharp / fsharp/fslang-suggestions

Improve the syntax of Statically Resolved Type Parameters

Open
#1,428 6 comments 1 reaction 0 assignees View on GitHub
area: srtp-and-constraints probably not
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I propose we improve the syntax of Statically Resolved Type Parameters

**The existing way of approaching this problem in F# is:**

As an example say we have the following:

```fsharp
type MyType = { Name: string; Age: int }
```
and we have a function that works with Name and Age fields, but we don't want to limit the function to just one type, and we don't want to go down the inheritance road, so we choose SRTP:

```fsharp
let inline giveMeNameAndAge (a: 'T when 'T: (member Name: string) and 'T: (member Age: int)) =
printfn $"Name: {a.Name}, Age: {a.Age}"
```

This is an amazing language feature, but any SRTP function with more than two fields becomes very hard to write and read. It would be great if we had a special easy syntax for this scenario, for example we could do this this instead:

```fsharp
// Maybe with ##
let inline giveMeNameAndAge (a : ##{| Name: string; Age: int |}) =

// Or with #>
let inline giveMeNameAndAge (a : #>{| Name: string; Age: int |}) =

// Or if we have some type, why not use it
let inline giveMeNameAndAge (a : ##MyType) =
```

**Why not just create two parameters?**
I explained a scenario [here](https://github.com/dotnet/fsharp/discussions/18294), long story short, if we want to create reusable validation functions that would work on any of our form models that have the required fields (without introducing a complex inheritance model), SRTP is our only choice.

**Related suggestions:** (put links to related suggestions here)
I'm not sure if [this one](https://github.com/fsharp/fslang-suggestions/issues/11) is related.

## Affidavit (please submit!)

Please tick these items 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] 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](https://github.com/dotnet/fsharp)
* [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
* [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

Please tick all that apply:
* [x] 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 :+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

No implementation files, tests, or entry points are identified. Start by reviewing the linked discussion and related suggestion to understand existing constraints and prior proposals. Done would require settling one SRTP syntax and its language-design semantics before implementation can be scoped.

Written by the indexing model from the issue text.

Assessment

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.