This is not about windows forms at all it's here only for the "background".
I was toying around Windows Forms when I got an error on an AddRange
for a MenuStrip.Items
requiring to cast ToolStripMenuItem
into ToolStripItem
But I already have an AddRange
for a Form.Controls
before which didn't require casts.
After a little experimentation I managed to find that the error occurs when there are multiple overload for that AddRange
so I tried to validate my thought :
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
[|Bar ()|] :> Foo[]
it doesn't work. – Athelstanlet foos : Foo [] = [| Bar () |]
? it's a wild guess but that works because it's another use of flexibility we give the type and there is an upcast between the two ; in[| Bar ()|] :> Foo []
it's the whole array we try to upcast – Rickrickard([| "test" |] : obj[])
vs.([| "test" |] :> obj[])
. See https://mcmap.net/q/1473077/-obj-and-string-as-parameters. – Annatto