June 16, 2010 Re: enforce()? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Tue, 15 Jun 2010 22:23:15 -0400, Andrei Alexandrescu <SeeWebsiteForEmail@erdani.org> wrote: > bearophile wrote: >> I have counted about 200 usages of std.contracts.enforce() inside >> Phobos. Can you tell me what's the purpose of enforce() in a language >> that has built-in Contract Programming? > > You need to read TDPL for that :o). > >> And what are the purposes of std.contracts.AssumeSorted()? Is it >> useful for something? > > AssumeSorted was an experiment. I think it has drawbacks that I don't know how to address, so I'll retire it. Hm... what are the drawbacks (besides it not being enforced)? I thought it was a good solution. -Steve | |||
June 16, 2010 Re: enforce()? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | On Wed, 16 Jun 2010 05:28:46 -0400, Ary Borenszweig <ary@esperanto.org.ar> wrote:
> On 06/16/2010 04:15 PM, Walter Bright wrote:
>> Ali Çehreli wrote:
>>> bearophile wrote:
>>>> I have counted about 200 usages of std.contracts.enforce() inside
>>>> Phobos. Can you tell me what's the purpose of enforce() in a language
>>>> that has built-in Contract Programming?
>>>
>>> I can see two benefits:
>>
>> The difference is not based on those 3 points, but on what Andrei wrote
>> here. Contracts and error checking are completely distinct activities
>> and should not be conflated.
>
> Could you please explain them? There are many people here that don't understand the difference between these two concepts (including me). So maybe we are too dumb, maybe those concepts are not generally known or maybe the explanation is not very well clear in the documentation.
I think of enforce as a convenient way translating an error in an expectation to an exception in a single expression.
For example, take some system call that returns -1 on error, you could do this:
if(result < 0)
throw new Exception("oops!");
or you could do this:
enforce(result >= 0, "oops!");
Think of enforce as "throw if"
And in fact, I think there's an errnoEnforce which throws a standard exception with the string error from the system.
I'd say the difference between enforce and assert is exactly what Andrei said -- enforce is meant to catch errors that can occur during normal operation. Assert is meant to catch errors that are not expected during normal operation. Assert's more like a sanity check. Also, assert is turned off in release mode, enforce is left on.
-Steve
| |||
June 16, 2010 Re: enforce()? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Wed, 16 Jun 2010 06:55:21 -0400, Steven Schveighoffer wrote: > On Wed, 16 Jun 2010 05:28:46 -0400, Ary Borenszweig <ary@esperanto.org.ar> wrote: > >> On 06/16/2010 04:15 PM, Walter Bright wrote: >>> Ali Çehreli wrote: >>>> bearophile wrote: >>>>> I have counted about 200 usages of std.contracts.enforce() inside >>>>> Phobos. Can you tell me what's the purpose of enforce() in a >>>>> language that has built-in Contract Programming? >>>> >>>> I can see two benefits: >>> >>> The difference is not based on those 3 points, but on what Andrei wrote here. Contracts and error checking are completely distinct activities and should not be conflated. >> >> Could you please explain them? There are many people here that don't understand the difference between these two concepts (including me). So maybe we are too dumb, maybe those concepts are not generally known or maybe the explanation is not very well clear in the documentation. > > I think of enforce as a convenient way translating an error in an expectation to an exception in a single expression. > > For example, take some system call that returns -1 on error, you could do this: > > if(result < 0) > throw new Exception("oops!"); > > or you could do this: > > enforce(result >= 0, "oops!"); > > Think of enforce as "throw if" It also adds a file and a line number to the error message, so the problem is easier to track down. Very handy. :) > And in fact, I think there's an errnoEnforce which throws a standard exception with the string error from the system. That's right, and there's even an enforceEx() which lets you specify which exception type to throw: http://digitalmars.com/d/2.0/phobos/std_contracts.html#enforceEx -Lars | |||
June 16, 2010 Re: enforce()? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 2010-06-16 05:15:24 -0400, Walter Bright <newshound1@digitalmars.com> said: > The difference is not based on those 3 points, but on what Andrei wrote here. Contracts and error checking are completely distinct activities and should not be conflated. True. Yet, enforce is inside std.contracts. If that isn't conflating the two concepts I wonder what it is. :-) -- Michel Fortin michel.fortin@michelf.com http://michelf.com/ | |||
June 16, 2010 Re: enforce()? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Steven Schveighoffer wrote: > Think of enforce as "throw if" > So why not concatenating the two and rename it to exactly 'throwif'? Self descriptive is better than cryptic 'enforce'. -- Alex Makhotin, the founder of BITPROX, http://bitprox.com | |||
June 16, 2010 Re: enforce()? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Steven Schveighoffer, el 16 de junio a las 06:55 me escribiste: > On Wed, 16 Jun 2010 05:28:46 -0400, Ary Borenszweig <ary@esperanto.org.ar> wrote: > > >On 06/16/2010 04:15 PM, Walter Bright wrote: > >>Ali Çehreli wrote: > >>>bearophile wrote: > >>>>I have counted about 200 usages of std.contracts.enforce() inside > >>>>Phobos. Can you tell me what's the purpose of enforce() in a language > >>>>that has built-in Contract Programming? > >>> > >>>I can see two benefits: > >> > >>The difference is not based on those 3 points, but on what Andrei wrote here. Contracts and error checking are completely distinct activities and should not be conflated. > > > >Could you please explain them? There are many people here that don't understand the difference between these two concepts (including me). So maybe we are too dumb, maybe those concepts are not generally known or maybe the explanation is not very well clear in the documentation. > > I think of enforce as a convenient way translating an error in an expectation to an exception in a single expression. > > For example, take some system call that returns -1 on error, you could do this: > > if(result < 0) > throw new Exception("oops!"); > > or you could do this: > > enforce(result >= 0, "oops!"); > > Think of enforce as "throw if" So maybe throw_if() would be a better name =) Anyway, I think enforce() is poisson, because it make the programmer to not think about errors at all, just add and enforce() and there you go. But when you need to be fault tolerant, is very important to know what's the nature of the error, but thanks to enforce(), almost every error is a plain Exception, no hierarchy, no extra info, all you can do to get a little more info about what happened is to parse the exception string, and that's not really an option. > And in fact, I think there's an errnoEnforce which throws a standard exception with the string error from the system. That's the only useful case of enforce, because it includes the *important* information (the actual errno). There is also enforceEx!(), to use a custom exception, which practically nobody uses (I counted only 4 uses in phobos). -- Leandro Lucarella (AKA luca) http://llucax.com.ar/ ---------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------- Y será el día en que la electricidad deje de ser rayo y sea depilador femenino. -- Ricardo Vaporeso | |||
June 16, 2010 Re: enforce()? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Michel Fortin | Michel Fortin wrote:
> On 2010-06-16 05:15:24 -0400, Walter Bright <newshound1@digitalmars.com> said:
>
>> The difference is not based on those 3 points, but on what Andrei wrote here. Contracts and error checking are completely distinct activities and should not be conflated.
>
> True.
>
> Yet, enforce is inside std.contracts. If that isn't conflating the two concepts I wonder what it is. :-)
You're right! I think Lars' suggestion is sensible - we should move enforce to object. Better yet we should find a better name for std.contracts. Ideas?
Andrei
| |||
June 16, 2010 Re: enforce()? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Alex Makhotin | Alex Makhotin wrote:
> Steven Schveighoffer wrote:
>> Think of enforce as "throw if"
>>
>
> So why not concatenating the two and rename it to exactly 'throwif'?
> Self descriptive is better than cryptic 'enforce'.
Well throwif describes mechanism and enforce describes intent. After all assert is not abortif :o).
Andrei
| |||
June 16, 2010 Re: enforce()? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Leandro Lucarella | Leandro Lucarella wrote: > Steven Schveighoffer, el 16 de junio a las 06:55 me escribiste: >> On Wed, 16 Jun 2010 05:28:46 -0400, Ary Borenszweig >> <ary@esperanto.org.ar> wrote: >> >>> On 06/16/2010 04:15 PM, Walter Bright wrote: >>>> Ali Çehreli wrote: >>>>> bearophile wrote: >>>>>> I have counted about 200 usages of std.contracts.enforce() inside >>>>>> Phobos. Can you tell me what's the purpose of enforce() in a language >>>>>> that has built-in Contract Programming? >>>>> I can see two benefits: >>>> The difference is not based on those 3 points, but on what Andrei wrote >>>> here. Contracts and error checking are completely distinct activities >>>> and should not be conflated. >>> Could you please explain them? There are many people here that >>> don't understand the difference between these two concepts >>> (including me). So maybe we are too dumb, maybe those concepts are >>> not generally known or maybe the explanation is not very well >>> clear in the documentation. >> I think of enforce as a convenient way translating an error in an >> expectation to an exception in a single expression. >> >> For example, take some system call that returns -1 on error, you >> could do this: >> >> if(result < 0) >> throw new Exception("oops!"); >> >> or you could do this: >> >> enforce(result >= 0, "oops!"); >> >> Think of enforce as "throw if" > > So maybe throw_if() would be a better name =) > > Anyway, I think enforce() is poisson, Indeed it is a bit fishy :o). > because it make the programmer to > not think about errors at all, just add and enforce() and there you go. > But when you need to be fault tolerant, is very important to know what's > the nature of the error, but thanks to enforce(), almost every error is > a plain Exception, no hierarchy, no extra info, all you can do to get > a little more info about what happened is to parse the exception string, > and that's not really an option. I think there is no real need for exception hierarchies. I occasionally dream of eliminating all of the useless exceptions defined left and right in Phobos. >> And in fact, I think there's an errnoEnforce which throws a standard >> exception with the string error from the system. > > That's the only useful case of enforce, because it includes the > *important* information (the actual errno). > > There is also enforceEx!(), to use a custom exception, which practically > nobody uses (I counted only 4 uses in phobos). I'd be hard pressed to find good examples of exception hierarchy use. Everybody talks about them but I've seen none. The fact that the coder doesn't need to think hard to use enforce() effectively is a plus, not a minus. An overdesigned enforce that adds extra burden to its user would have been a mistake. Andrei | |||
June 16, 2010 Re: enforce()? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | == Quote from Andrei Alexandrescu (SeeWebsiteForEmail@erdani.org)'s article > Everybody talks about them but I've seen none. > The fact that the coder doesn't need to think hard to use enforce() > effectively is a plus, not a minus. An overdesigned enforce that adds > extra burden to its user would have been a mistake. > Andrei IMHO the presence of a simple method of handling errors, even if it's far from perfect, is a good thing. If you have to think about a whole exception hierarchy every time you hit a possible error condition in your code, you tend to put this tedious task off until forever, leading to programs that fail for unknown reasons because some error condition was never reported. Well-designed exception hierarchies are nice, but forcing their use all the time would be making the perfect the enemy of the good. Furthermore, I love enforce() because sometimes I want just some subset of assertions checked in release mode, usually whichever ones can be checked at negligible performance cost. I tend to use it a lot as an assert-even-in-release-mode function. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply