question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Duplicate method ... same signature" error occurs for member names other than op_Implicit

See original GitHub issue

Consider 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’.

image

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:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dsymecommented, Oct 10, 2022

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

0reactions
NinoFloriscommented, Oct 10, 2022

I have a feeling that’s been proposed before, need to search for it.

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found