On Wed, Aug 10, 2011 at 7:29 AM, Robert Fischer <[email protected]> wrote: > Can you define "contra-" and "co-" variance for me in the context of > structural (not nominal) typing? >
You get co-/contra-variance any time you have subtyping- any time you have a situation where all B's are also A's, but not all A's are B's. So if A is the type of objects having methods foo and bar, and B is the type of objects having foo, bar, and baz methods (with foo and bar having the same signatures as A's foo and bar), then B is a subtype of A. The fact that the subtyping relationship was introduced structurally or nominally is irrelevant. In terms of theory, there is no difference between structural typing and nominal typing. Introducing a new structural type is the equivalent to introducing a new uniquely named nominal interface, and having the interface applied to all applicable types. In practice, introducing that new interface and applying it to all necessary classes ranges in difficulty from annoying to impossible, so in practice there is a huge difference (and structural aka duck typing wins big time). > Specifically, can you show a situation where contra- or co- variance > are problems when using structural typing without mutable data > structures? > What are the types for List.cons and List.first? Or Map.add and Map.get? And you're going to have mutable containers, even if they're only ref cells. Even Haskell has MVars and TVars and the like. Brian -- You received this message because you are subscribed to the Google Groups "JVM Languages" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/jvm-languages?hl=en.
