Skip to content

Overload resolution for Result cannot be determined when a method for Nullable exists #12860

@pedersorensen

Description

@pedersorensen

If a type has a method with overloads for both Nullable<T> and Result<T, TError>, the overload for Result cannot be resolved by the compiler.

Repro steps

  1. Create a type like the following (method bodies are not important, only the signatures)
type M() =
    static member A(n: Nullable<'T>) = ()
    static member A(o: Option<'T>) = ()
    static member A(r: Result<'T, 'TError>) = ()
  1. Call the various overloads:
M.A(Some 3)
M.A(Nullable 3)
M.A(Ok 3) // <-- Does not compile
M.A(Result<int, string>.Ok 3) // <-- Nor when the type of Result is fully specified

The last two lines yield the following compiler error

error FS0041: A unique overload for method 'A' could not be determined based on type information prior to this program point. A type annotation may be needed.

Known type of argument: Result<int,string>

Candidates:
 - static member M.A: n: Nullable<'d> -> bool when 'd: (new: unit -> 'd) and 'd: struct and 'd :> ValueType
 - static member M.A: r: Result<'a,'b> -> bool

The same behavior occurs for instance methods with a similar setup.

Expected behavior

All three methods should be callable.

Actual behavior

Calling the overload that takes a Result<T, TError> does not compile.

Known workarounds

  1. Use /langversion:4.7

  2. Using a named parameter resolves the correct overload, provided the names of the parameters of the different overloads are different:

    let c1 = M.A(r = Ok 3)

    If the Nullable<T> overload is removed, the Result<T, TError> overload can be called just fine.

Related information

  • OS: Windows 11
  • .NET Runtime kind .NET 6
  • Visual Studio version 17.1.1
  • F# version 6.0

Metadata

Metadata

Assignees

Labels

Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.Regression

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions