"Duplicate method ... same signature" error occurs for member names other than op_Implicit
See original GitHub issueConsider this code:
type A =
| A of int
static member M(A x) : int = x
static member M(A x) : string = x.ToString()
This leads to the following compilation error:
Duplicate method. The method ‘M’ has the same name and signature as another method in type ‘A’.
I assume “signature” refers to the parameters and ignores the return type. However, using the name op_Implicit
works fine:
type A =
| A of int
static member op_Implicit(A x) : int = x
static member op_Implicit(A x) : string = x.ToString()
Why should it depend on the name? Is this a bug?
My use-case is that I am using some SRTP helpers to convert between compatible types, but I’d like to move away from relying on op_Implicit
for these helpers in order to force myself to use these helpers. With op_Implicit
, I get automatic conversion at method call sites (but I don’t want to enable the warning/error for this, since I want that implicit conversion for some built-in .NET APIs/types).
Related information
- Windows 11
- Rider 2022.2.3
Issue Analytics
- State:
- Created a year ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Java: duplicate method signature in nested class
At first method signature is not the combination of return type and method name it is method name and parameters.
Read more >'ClassFormatError: Duplicate method name' during runtime ...
I want that two statement(variable, method) including lambda was added Testjava class. So after code was written as below code, I compiled code....
Read more >Duplicate method error
ClassFormatError : Duplicate method name&signature in class file main/Foo. I guess this is because the compiler implicitly creates getFoo() ...
Read more >C# | How to Implement Multiple Interfaces Having Same ...
In this example, we take two interfaces named as G1 and G2 with the same method name. Now implement these interfaces in a...
Read more >Chapter 8. Classes
This situation can occur if the class would have as members two abstract methods that have the same method signature (§8.4.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is by design - overloading by return type is only allowed for op_Implicit and op_Explicit, it’s simply not allowed for other members.
Return types are only taken into account on SRTP constraint solving.
op_Implicit/Explicit are just so different to other members as they nearly only ever invoked through this constraint solving or other F# 6 implicit conversion resolution. Plus you simply can’t define useful sets of these overloads without overloading by return type.
For other methods, we ban overloading by return type because it would give the false impression that return types are taken into account bin overload resolution.
I agree there’s a corner case migration problem here making it hard to migrate away from op_Implicit. I guess some attribute to allow overloading by return type could make sense. I have a feeling that’s been proposed before, need to search for it.
Anyway, currently by design and would need to go via a language suggestion
You’re correct, it lives in the trait extension PR https://github.com/dotnet/fsharp/pull/6805 as
AllowOverloadOnReturnTypeAttribute
. Which would close the proposal https://github.com/fsharp/fslang-suggestions/issues/820