'This' type

Howard Lovatt howard.lovatt at iee.org
Sun Mar 15 03:20:59 PDT 2009


I am not sure I completely follow the proposal, but if the proposal is
something like self types in Eiffel or Scala then they can have
problems. In the Eiffel book, I think it is ed. 3 that has self types,
they are introduced in a Skier example. The example is organising a
High School Ski Trip and you don't want the boys sharing with the
girls, so you want the roommate() method to have type this (self in
Eiffel), so that Boy's roommmate returns a Boy and Girl's roommate()
returns a Girl. Something like:

interface Skier { This roommate(); void setRoommate(This roomie); ... }

class Boy implements Skier { Boy roommate() ... }

class Girls implements Skier { Girl roommate() ... }

Now they introduce the concept of a Ranked skier and have classes
RankedBoy and RankedGirl, something like this:

interface Ranked { int ranking(); }

class RankedBoy extends Boy implements Ranking { RankedBoy roommate() ... }

...

The problem is that now only RankedBoys can share with RankedBoys;
which was not the intention in the example and is an extremely
difficult to find bug. This shows that even *expert* programmers can
make mistakes with self types. Therefore self types seem to solve some
problems, but introduce others.



More information about the coin-dev mailing list