F# constructor doesn't accept tuples? -
F# constructor doesn't accept tuples? -
i tried utilize tuple create new instance of class defined in f#. duplicate problem, tried next code.
type test(x: int, y:int) = allow distance = x * x + y * y |> float |> sqrt new (x: int, y:int, z:int) = new test(x, y) allow args = 1, 2 allow test2 = new test(args)
it complains
error 1 fellow member or object constructor 'test' not take 1 argument(s). overload found taking 2 arguments.
if remove non-default constructor, things fine. don't understand why becomes two/three arguments instead of tuples.
thank much.
this subtle, per spec. here's old email response dug asked similar question:
...
at play, there (subtle) difference between "tuples" (in f# language) , "syntactic tuples" (in f# specification).
method application resolution different when there overloads. if there none, decomposition of argument (i.e. "stuff" specified between ( , ) in method invocation) tuple form not happen, compiler happy , "oh ok, ctor myclass takes 1 argument (a tuple) , seeing 1 argument ("tuple" in code) i’ll utilize that".
whereas, when have 2 overloads, rule above not apply anymore , compiler seek decompose argument tuple form (which in case resolve in like: "oh ok, there 1 argument tuple. wait, have 2 overloads. , list of argument (one element, tuple) not match either argument list, hence error message"
at least, that’s interpretation of f# specification, section 14.4.
f# constructor tuples
Comments
Post a Comment