[contest] Is a Cow an animal ++

bearophile bearophileHUGS at lycos.com
Thu Sep 30 05:04:55 PDT 2010


Norbert Nemec:

> Which confirms my original position: the final three tests in the 
> contest are flawed by concept.

Those tree tests may be impossible to do, but they are not flawed (and they are not impossible to do, see below).


> If the actions of eating and slaughtering 
> are supposed to happen at run-time, no compiler in the world can 
> reliably detect them happening twice.

The Rust language (that doesn't exist yet, by Mozilla) will probably be able to do that. It implements the idea of "typestate". This is the old original paper about typestates, Typestate: A Programming Language Concept for Enhancing Software Reliability", by Robert E. Strom and Shaula Yemini, 1986:
http://www.cs.cmu.edu/~aldrich/papers/classic/tse12-typestate.pdf

In a language like D a type has no state, a int value is always an int. In a language that supports typestate you may think of a File type as a finite state machine, in each state of this FSM you are able to access only a subset of the File methods, when the state of the File type is open, you can't access the 'open' method, and so on. The transitions between those states don't happen at runtime, they happen in the code space. The compiler must analyse the code, perform some flow analysis and change the state of the File type in different parts of the program. Before the File.open() the File type is in the start state, then in the section of the code after the call to open the type of that File instance is in the opened state, and so on.

So the FSM of a Cow state is just two states, alive and dead/eaten, and the compiler that supports typestates disallows the eat() method in the section of the code where the state of that Cow instance is Eaten.

I'd probably like to have typestates in D3 too.

Bye,
bearophile


More information about the Digitalmars-d mailing list