dotnet / dotnet/fsharp

Better error message when method call is resolved to a C# extension method instead of the type own one

Open
#3,652 4 comments 0 reactions 0 assignees View on GitHub
Area-Diagnostics Feature Improvement
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

According to C# extension methods documentation, a method call wouldn't resolve to an extension method if the type implements a method with the same signature.
However in F# I get type error due to resolve to an extension method instead of the type own one. I'm not sure whether it is a language design choice or a bug.

I'm facing it when I open a namespace that contains classes with extension methods, which makes me avoid opening such namespaces in F# projects.

Here's a repro solution: [ExtensionMethodPriority.zip](https://github.com/Microsoft/visualfsharp/files/1334772/ExtensionMethodPriority.zip).

```csharp
using System.Collections.Generic;

namespace CSharpLib
{
public static class CollectionUtil
{
public static TValue TryGetValue(this IDictionary dictionary, TKey key)
{
return default(TValue);
}
}
}
```

```fsharp
module FSharpLib

open CSharpLib
open System.Collections.Generic

let d = Dictionary()

match d.TryGetValue("foo") with
| true, value -> ()
| _ -> ()
```

```
/Users/eugene/Developer/TestSolutions/ExtensionPriorityBug/FSharpLib/Library.fs(9,3):
error FS0001: This expression was expected to have type 'string' but here has type ''a * 'b'
```

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.