Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upcast mandatory when there are different overload

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

like image 266
Sehnsucht Avatar asked Jul 07 '16 11:07

Sehnsucht


1 Answers

After reading the 14.4.3 F# spec (hinted by Gustavo, kudos to him)

The F# compiler determines whether to insert flexibility after explicit instantiation, but before any arguments are checked.

I understand that flexibility is never inserted for a method which have overloads because it would need argument checking to choose.

like image 53
Sehnsucht Avatar answered Nov 07 '22 02:11

Sehnsucht