fsharp / fsharp/fslang-suggestions

allow named fields in active patterns

Open
#1,341 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I'm trying to use a library which exposes active patterns instead of the data types with their internal definition.

https://github.com/fsprojects/FSharp.Data/blob/8a6688f34abede0a80306e6c802601ef74edf473/src/FSharp.Data.Html.Core/HtmlActivePatterns.fs

The experience isn't as good as direct access to the representation, but it could be improved:

I suggest we allow active patterns to define field names:

```fsharp
module HtmlActivePatterns =
let (|HtmlElement(name,attributes,elements)|HtmlText(content)|HtmlComment(content)|HtmlCData(content)|) (node: HtmlNode) =
match node with
| HtmlNode.HtmlText content -> HtmlText(content)
| HtmlNode.HtmlComment content -> HtmlComment(content)
| HtmlNode.HtmlCData content -> HtmlCData(content)
| HtmlNode.HtmlElement (name, attributes, elements) -> HtmlElement(name, attributes, elements)
```

Here, I defined the field names explicitly (this is just a suggested syntax).

Possibly, if those aren't defined, but the value given is a plain identifier like in the code above, it would put the equivalent of `nameof identifier` for the name.

Possibly, we would issue a warning (on by default in new projects), when the names are not defined.

If we don't want to change the representation, we may make this work like C# value tuple with names work.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples#tuple-field-names

The deconstruction syntax would allow partial deconstruction, using the usual syntax with explicit field names instead of positional one.

We may also allow the constructor syntax to specify the names (and have a check that enforces consistency):

```fsharp
module HtmlActivePatterns =
let (|HtmlElement|HtmlText|HtmlComment|HtmlCData|) (node: HtmlNode) =
match node with
| HtmlNode.HtmlText content -> HtmlText(content=content)
| HtmlNode.HtmlComment content -> HtmlComment(content=content)
| HtmlNode.HtmlCData content -> HtmlCData(content=content)
| HtmlNode.HtmlElement (name, attributes, elements) -> HtmlElement(name=name, attributes=attributes, elements=elements)
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the linked FSharp.Data HtmlActivePatterns.fs example and the issue's proposed active-pattern and C# value-tuple field-name syntax. No implementation files or tests are named; done would require an agreed syntax and semantics for named fields, plus corresponding compiler changes 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.