Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tuples in type members using F#

I have a simple question. Why does this not work?

type Test1() =
  member o.toTuple = 1.,2.,3.

type Test2() =
  member o.test (x: float, y: float, z: float) = printfn "test"
  member o.test (x: Test1) = o.test x.toTuple

The error is:

Type constraint mismatch. The type float * float * float is not compatible with type Test1 The type 'float * float * float' is not compatible with the type 'Test1'

and

The type 'float * float * float' is not compatible with the type 'Test1'

like image 894
Oldrich Svec Avatar asked Dec 02 '25 08:12

Oldrich Svec


2 Answers

This doesn't work because the first member test is considered as a multiple argument method in case of overloading. If you need a tupled one, you have to add extra parentheses:

type Test2() =
    member o.test ((x: float, y: float, z: float)) = printfn "test"
    member o.test (x: Test1) = o.test x.toTuple

See explanation of Don Syme here.

Note that if you don't want to add extra parens, you still can deconstruct the tuple and use the multiple argument call:

type Test2() =
    member o.test (x: float, y: float, z: float) = printfn "test"
    member o.test (x: Test1) = let a,b,c = x.toTuple in o.test(a,b,c)
like image 82
Stringer Avatar answered Dec 04 '25 04:12

Stringer


Rename your first method in Type2 to something other than test. You second method is shadowing your first, and thus confusing the compiler.

type Test1() =
   member o.toTuple = 1.,2.,3.

type Test2() =
  member o.print (x: float, y: float, z: float) = printfn "test"
  member o.test (x: Test1) = o.print x.toTuple
like image 30
pblasucci Avatar answered Dec 04 '25 05:12

pblasucci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!