fsharp / fsharp/fslang-suggestions

Generate F# 9-style Is* property for single case discriminated union

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

Description

**I propose we** generate an Is* boolean property for single case unions, as F# 9 does today for multi-case unions.

```fsharp
module Types

open System

type AssetType = Image // Intention is to add to this definition in the future as asset types are needed

type Asset =
{ Type : AssetType
SourceURL : Uri }

// I want to write a function like this, such that even if new cases are added in the future, this function gives me only the ones of type `Image`
let onlyImages assets =
assets
|> List.filter _.Type.IsImage // as is, this line errors with: 'The type 'AssetType' does not define the field, constructor, or member 'IsImage'. Maybe you want one of the following: Image'
```

**The existing way of approaching this problem in F# is** to add a throwaway associated value and pattern match, like so:

```fsharp
type AssetType = Image of int // could be any type, in theory

let onlyImages assets =
assets
|> List.filter (fun ({ Type = (Image _) }) -> true)
```

## Pros and Cons

**The advantages of making this adjustment to F# are** that the property is generated for all cases of a DU, not just when there are multiple. While in theory the Is property is redundant for that case since it is guaranteed to be true, this enhancement improves the behavior for "planning for future cases" by allowing to use the same syntax, instead of having to do a hacky workaround just for the single case scenario

**The disadvantages of making this adjustment to F# are** that perhaps this is a small thing and maybe the juice isn't worth the squeeze just for this extra consistency?

## Extra information

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

## 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
* [x] 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

Start by reviewing the proposal and comparing single-case discriminated unions with the existing F# 9-style Is* properties for multi-case unions. Done means determining and implementing the language behavior so a single-case union such as AssetType generates an IsImage property, with suitable validation 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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.