Skip to content

Fix #2564: op_Implicit overloads #2583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: fable3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Fable.Transforms/OverloadSuffix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion tests/Main/CustomOperatorsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,31 @@ type ToLength = ToLength with

let inline toLen x : string = (Unchecked.defaultof<ToLength> &. 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<px>
|> equal "1px"

testCase "second overload" <| fun () ->
toLen 1<em>
|> 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)