The Artima Developer Community
Sponsored Link

Versioning, Virtual, and Override
A Conversation with Anders Hejlsberg, Part IV
by Bill Venners with Bruce Eckel
September 15, 2003

<<  Page 2 of 3  >>


Incoming and Outgoing Contracts

Bill Venners: It sounds like your not as concerned that the person overriding the method won't correctly implement the promises you've made to the caller. You are worried about all the promises you have to make to the person overriding the method.

Anders Hejlsberg: I'm concerned with both, actually. Virtual has two sides to it: the incoming and the outgoing. People are very good about thinking about what the contract is on the incoming. They are very bad about thinking about the outgoing contract.

Bill Venners: What do you mean by incoming and outgoing contract?

Anders Hejlsberg: The incoming contract is what I use when call a method. It enables me to reason about what I am required to do before I call into the method and what has happened after the method returns. The outgoing contract is what I use when I override a method. If you look at most APIs they are actually very bad at documenting what you're supposed to do when you override a virtual method: What are the invariants before you're called? What should be true after? Which methods can you not call from your implementation? And so on. I think it is very dangerous to say that by default all methods have both incoming and outgoing aspects. It is dangerous to trust everyone to write good documentation about when to override and what the invariants are when overriding.

I can demonstrate to you a very real world versioning problem, one that indeed we see now from experience with Java. Whenever they ship a new version of the Java class libraries, breakage occurs. Whenever they introduce a new method in a base class, if someone in a derived class had a method of that same name, that method is now an override—except if it has a different return type, it no longer compiles. The problem is that Java, and also C++, does not capture the intent of the programmer with respect to virtual.

<<  Page 2 of 3  >>

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use