From 014fb592260e472f69f98525c2da3e1b91c7616e Mon Sep 17 00:00:00 2001 From: Alfonso Garcia-Caro Date: Mon, 1 Nov 2021 15:31:00 +0900 Subject: [PATCH] Fix #2564: op_Implicit overloads --- src/Fable.Transforms/OverloadSuffix.fs | 3 +++ tests/Main/CustomOperatorsTests.fs | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Fable.Transforms/OverloadSuffix.fs b/src/Fable.Transforms/OverloadSuffix.fs index cb2991588e..7721e2bc59 100644 --- a/src/Fable.Transforms/OverloadSuffix.fs +++ b/src/Fable.Transforms/OverloadSuffix.fs @@ -150,6 +150,9 @@ let getHash (entity: FSharpEntity) (m: FSharpMemberOrFunctionOrValue) = if m.CurriedParameterGroups.Count <> 1 then "" else let paramTypes = m.CurriedParameterGroups.[0] |> Seq.map (fun p -> p.Type) |> Seq.toList + let paramTypes = + if m.CompiledName = "op_Implicit" then paramTypes @ [m.ReturnParameter.Type] + else paramTypes if hasEmptyOverloadSuffix paramTypes then "" else // Generics can have different names in signature diff --git a/tests/Main/CustomOperatorsTests.fs b/tests/Main/CustomOperatorsTests.fs index 2c2e554b2f..c0ebcb5305 100644 --- a/tests/Main/CustomOperatorsTests.fs +++ b/tests/Main/CustomOperatorsTests.fs @@ -88,15 +88,31 @@ type ToLength = ToLength with let inline toLen x : string = (Unchecked.defaultof &. x) D1 D2 D3 +type MyType = + { Field : int} + static member op_Implicit(s: MyType):int = s.Field + 2 + static member op_Implicit(s: MyType):string = $"Field is %i{s.Field}" + +#nowarn "3391" + let operatorsForOverloads = [ testCase "first overload" <| fun () -> toLen 1 |> equal "1px" + testCase "second overload" <| fun () -> toLen 1 |> equal "1em" + + testCase "op_Implicit can be overloaded" <| fun () -> + let takeInt(x: int) = x + let takeString(x: string) = x + let x = { MyType.Field = 5 } + // TODO Remove MyType.op_Implicit with F# 6 + takeInt(MyType.op_Implicit x) |> equal 7 + takeString(MyType.op_Implicit x) |> equal "Field is 5" ] let tests = - testList "Miscellaneous" + testList "Custom operators" (typeOperators @ moduleOperators @ operatorsAsFunctions @ operatorsForOverloads)