dotnet / dotnet/fsharp

[Error Messages] Unhelpful error when trying to augment a generic type with concrete type parameters

Open
#3,394 1 comment 0 reactions 0 assignees View on GitHub
Feature Improvement Theme-Simple-F#
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

```F#
module X =

open System

// this compiles
type Tuple<'T1, 'T2> with
member x.I() = 1

// this fails with:
// error FS0010: Unexpected identifier in type name. Expected infix operator, quote symbol or other token.
type Tuple with
member x.I() = 1
```

### Fix

```
open System.Runtime.CompilerServices

[]
type TupleEx() =
[]
static member inline I1(self: Tuple) = 1
[]
static member inline I2(self: int*_) = 1

let x = (1, "")
let i1 = x.I1()
let i2 = x.I2()

```

It would be nice if the first code snippet failed with an explicit error message, something like

> generic types with concrete type parameters can not be extended. You may want to use explicit extension methods using the ``[]`` Attribute. See https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/type-extensions

Note: the second snippet fails in the released version, but compiles on master due to the tuple fixes by dsyme, thank you!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.