0 people like it.

Why automatic upcasting would not work

N/A

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
// Consider two distinct interfaces
type IA = interface end
type IB = interface end

// And two classes that implement both of the interfaces
type First() = 
  interface IA
  interface IB

type Second() = 
  interface IA
  interface IB

// Now, what implicit upcast should the compiler insert here?
// The return type could be either IA, IB or obj, but there is 
// no _unique_ solution.
//
// (This is an error in F# and it cannot be easily fixed by 
// trying to identify the most specific common supertype, because
// there is no single common supertype)
let test = 
  if 1 = 1 then First()
  else Second()
type IB

Full name: Script.IB
Multiple items
type First =
  interface IB
  interface IA
  new : unit -> First

Full name: Script.First

--------------------
new : unit -> First
type IA

Full name: Script.IA
Multiple items
type Second =
  interface IB
  interface IA
  new : unit -> Second

Full name: Script.Second

--------------------
new : unit -> Second
val test : First

Full name: Script.test

More information

Link:http://fssnip.net/ix
Posted:3 years ago
Author:
Tags: na