Mandatory Upcast parameter if there is another overload

This is not about window shapes, but only for the "background".

I played in Windows Forms when I got an error in AddRange for MenuStrip.Items requiring dropping ToolStripMenuItem in ToolStripItem

But I already have AddRange for Form.Controls , before which no drops were required.

After a little experiment, I managed to find that an error occurs when there are several overloads for this AddRange , so I tried to check my thoughts:

 type Foo () = class end type Bar () = inherit Foo () type FooCollection () = class end // not really necessary type Test1 () = member __.AddRange (col: FooCollection) = () // could be an int or anything instead member __.AddRange (foos: Foo []) = () type Test2 () = member __.AddRange (foos: Foo []) = () let lst1, lst2 = Test1 (), Test2 () lst1.AddRange [|Bar ()|] // error: have to explicitely cast => [|Bar () :> Foo|] lst2.AddRange [|Bar ()|] // works 

The question is simply why; from my point of view, the call is not ambiguous

+8
casting method-overloading f # overload-resolution ambiguous-call
source share
1 answer

After reading 14.4.3 F # spec (scheduled by Gustavo , he likes it)

The F # compiler determines whether to embed flexibility after explicitly creating the instance, but before the arguments are checked.

I understand that flexibility is never inserted for a method that has overloads because it will require argument checking to select it.

+2
source share

All Articles