[contest] Is a Cow an animal ++

Justin Johansson no at spam.com
Mon Sep 27 05:50:53 PDT 2010


On 27/09/2010 10:26 PM, bearophile wrote:
> This is a harder variant of an old and very simple OOP problem:
> http://merd.sourceforge.net/pixel/language-study/various/is-a-cow-an-animal/
>
> The problem presents a hierarchy of types, some rules, and 11 tests, each one of them represents a bug because it contains a violation of the rules. You have to write the program in a way that the type system refuses as many of those 11 tests as possible at compile-time. There are many acceptable ways to write the program, for example you may use functional programming, so you are able to return different types, that enforce different rules, so it's not a contest, it's more like a demonstration.
>
> A basic Python implementation catches none of the 11 tests at compile time :-) A basic D implementation that I have written catches five of the 11 bugs at compile time:
> http://ideone.com/87q67
> I will try to write a more statically typed D version.
>
> The site shows four C++ versions, the fourth is able to catch 8 of the 11 bugs at compile-time. A type system that supports typestates is probably able to catch all 11 bugs (because for example the Cow type has two states, alive and eaten, and you can't perform some operations on a eaten cow, like eating it again), but probably it's very hard to do this with D.
>
> The fourth C++ version contains a line that I am am not even sure how to translate to D (maybe there is a workaround with template constraints):
> (void) static_cast<My_kind *>((Kind *) 0);
>
> Bye,
> bearophile

Accolades to you bearophile for using a subject annotation
i.e. [contest] for this post.  It would be nice if people
used, say, [license] or whatever as appropriate as an
annotation to discriminate their posts on the long running
"Andrei's Google Talk"thread which is more of a Gordian Knot
rather than a thread per se.

Anyway, my 2 cents about this type of problem is that much
of OOP is about trying too hard to model taxonomies in single
inheritance fashion, and this has obvious failing in the
real world.

Still I have not read your links and perhaps I'm shooting
from the foot so I will go off now and digest the problem
that you submit as a [contest]. :-)

Regards
JJ


More information about the Digitalmars-d mailing list