Why automatic upcasting would not work

N/A

Copy Source
Copy Link
Tools:
 1: // Consider two distinct interfaces
 2: type IA = interface end
 3: type IB = interface end
 4: 
 5: // And two classes that implement both of the interfaces
 6: type First() = 
 7:   interface IA
 8:   interface IB
 9: 
10: type Second() = 
11:   interface IA
12:   interface IB
13: 
14: // Now, what implicit upcast should the compiler insert here?
15: // The return type could be either IA, IB or obj, but there is 
16: // no _unique_ solution.
17: //
18: // (This is an error in F# and it cannot be easily fixed by 
19: // trying to identify the most specific common supertype, because
20: // there is no single common supertype)
21: let test = 
22:   if 1 = 1 then First()
23:   else Second()
Multiple items
type IB =
  interface
  end

Full name: Snippet.IB

--------------------
IB
type First =
  class
    interface IB
    interface IA
    new : unit -> First
  end

Full name: Snippet.First

  type: First
  implements: IB
  implements: IA
Multiple items
type IA =
  interface
  end

Full name: Snippet.IA

--------------------
IA
type Second =
  class
    interface IB
    interface IA
    new : unit -> Second
  end

Full name: Snippet.Second

  type: Second
  implements: IB
  implements: IA
val test : First

Full name: Snippet.test

  type: First
  implements: IB
  implements: IA

More information

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