A question about DbC

Jonathan M Davis jmdavisProg at gmx.com
Sat Oct 9 04:56:03 PDT 2010


On Saturday 09 October 2010 04:49:08 Denis Koroskin wrote:
> On Sat, 09 Oct 2010 15:32:26 +0400, Jonathan M Davis <jmdavisProg at gmx.com>
> 
> wrote:
> > On Saturday 09 October 2010 04:23:25 Denis Koroskin wrote:
> >> On Sat, 09 Oct 2010 15:06:40 +0400, Jonathan M Davis
> >> <jmdavisProg at gmx.com>
> >> 
> >> wrote:
> >> > On Saturday 09 October 2010 03:47:52 Denis Koroskin wrote:
> >> >> Why not just throw an exception and get a nice stack trace?
> >> > 
> >> > You get a stack trace anyway with an assertion failure. And sure, they
> >> > _could_
> >> > make it so that the only way to output anything from a contract is to
> >> > use an
> >> > exception, but not only would that be more of a pain than using
> >> > writeln(), but
> >> > it would mean that the only time you could output anything would be on
> >> > failure.
> >> > As it is, you can print something every time that a contract is run.
> >> 
> >> You
> >> 
> >> > couldn't do that with an exception.
> >> > 
> >> > - Jonathan M Davis
> >> 
> >> I could do the same within a function body.
> >> 
> >> Anyway, I don't see the discussion going anywhere, it's just a matter of
> >> preference and I don't really mind yours.
> > 
> > Well, regardless of what we think and what the pros and cons of the
> > situation
> > actually are, as I understand it, the whole reason that contracts aren't
> > pure is
> > so that you can use writeln() in them for debugging.
> > 
> > - Jonathan M Davis
> 
> I don't thinks so, it's rather the feature isn't fully implemented yet.
> For example, up until recently you couldn't throw Exceptions from
> contracts, and was only be able to use asserts. That got changed because
> it was an easy to violate rule (by calling some other function that
> throws, e.g. enforce). Why do you think Walter disallowed throwing from
> contracts (other than asserts) in first place?

IIRC, TDPL specifically says that the reason that contracts aren't pure is so 
that you can use writeln() for debugging. And as for exceptions, why _would_ you 
throw them from a contract? Contracts are supposed to contain assertions and 
whatever code is necessary to set up what's given to them. That's it. Exceptions 
are used for error handling, which contracts aren't supposed to do.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list