Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!spool.mu.edu!uunet!mcsun!ukc!pyrltd!tetrauk!rick From: rick@tetrauk.UUCP (Rick Jones) Newsgroups: comp.lang.eiffel Subject: Re: Covariance vs Contravariance. Message-ID: <1113@tetrauk.UUCP> Date: 13 Mar 91 13:26:10 GMT References: <860@puck.mrcu> Reply-To: rick@tetrauk.UUCP (Rick Jones) Organization: Tetra Ltd., Maidenhead, UK Lines: 55 In article <860@puck.mrcu> paj@uk.co.gec-mrc (Paul Johnson) writes: > >Another thought to add to the co/contra variance debate. Why not have >both? > > [detailed definition of the co/contra variant principles] > >Why not remove the restrictions on the argument types. Contravariant > ^^^^^^^^^^^^^^^^^^^^^^^ >typing may not be very useful, but there is no reason to disallow it >since it is always type safe. If the type checker can catch misuses >of covariant typing then the compiler can allow both. I'm not sure if you meant to say that, but you can't actually _remove_ the restrictions. What you can do is allow arguments to be redefined as either ancestors or descendants of the inherited definitions. I.e given a portion of an inheritance tree: A / \ / \ B D / / C then a function "foo (arg: B)" in some class may be redefined in a descendant as either "foo (arg: C)" (covariance), or "foo (arg: A)" (contravariance). It _cannot_ be redefined as "foo (arg: D)". I would agree in principle that since contravariance is guaranteed safe, there seems no reason to disallow it because covariance is supported. However, this leads to a logical anomaly. To take an extreme case, consider the following: class X declares: foo (arg: STRING) | | class Y redefines: foo (arg: ANY) -- legal by contravariance | | class Z redefines: foo (arg: TREE) -- legal by covariance If class Y does nothing else apart from redefining foo, then you could arguably say that class Z should be able to inherit directly from class X. This _would_ require removal of all type checks on arguments, which seems from a practical point of view to be a nonsense. It does suggest that allowing co- and contra- variance to coexist would lead in practice to more confusion than it resolved. -- Rick Jones, Tetra Ltd. Maidenhead, Berks, UK rick@tetrauk.uucp The dynamics of a freeway approximates to a shoal of fish in 1.5 dimensions