Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 01, 2002 Should This Be Legal? | ||||
---|---|---|---|---|
| ||||
I was doing a search through an array, and I wanted to assert that I definitely would find what I was looking for in the array. So I coded this, which crashed DLI 0.1.1: for(int foo=0; assert(foo<array.length); foo++) if(do_test(array[foo])) do_stuff(); Is this be legal? If not, why not? -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
November 01, 2002 Re: Should This Be Legal? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | Russ Lewis wrote:
> I was doing a search through an array, and I wanted to assert that I
> definitely would find what I was looking for in the array. So I coded
> this, which crashed DLI 0.1.1:
>
> for(int foo=0; assert(foo<array.length); foo++)
> if(do_test(array[foo]))
> do_stuff();
>
> Is this be legal? If not, why not?
assert isn't an expression, so no. This assertion is redundant anyway - in normal builds the array[foo] will do a bounds check, in release builds neither will be checked.
|
November 01, 2002 Re: Should This Be Legal? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Burton Radons | Burton Radons wrote: > Russ Lewis wrote: > > I was doing a search through an array, and I wanted to assert that I definitely would find what I was looking for in the array. So I coded this, which crashed DLI 0.1.1: > > > > for(int foo=0; assert(foo<array.length); foo++) > > if(do_test(array[foo])) > > do_stuff(); > > > > Is this be legal? If not, why not? > > assert isn't an expression, so no. This assertion is redundant anyway - in normal builds the array[foo] will do a bounds check, in release builds neither will be checked. ACK!!!! I forgot the key thing in the whole code! There was supposed to be a break if you match the test: for(int foo=0; assert(foo<array.length); foo++) if(do_test(array[foo])) { do_stuff(); break; } Does that make more sense now? -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
November 01, 2002 Re: Should This Be Legal? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:3DC2903C.660DA40C@deming-os.org... > I was doing a search through an array, and I wanted to assert that I definitely would find what I was looking for in the array. So I coded this, which crashed DLI 0.1.1: > > for(int foo=0; assert(foo<array.length); foo++) > if(do_test(array[foo])) > do_stuff(); > > Is this be legal? If not, why not? > No its not legal and should never be. apart from assert not being an expression assert is a check for something that is NOT expected to happen and should compile to nothing when non assert builds are built. here quite clearly foo can be larger than array.length; I assume your code should have been (where getting to the end would be an error) for(int foo=0; assert(foo<array.length); foo++) if(do_test(array[foo])) break; arrays should be bounds checked in asserting builds (IMHO) also asserts SHOULD not have side effects the code foo = 0; do { assert( ++foo < array.length ); count += array[foo]; } while( count < 100 ); should be a compiler warning (modification withing an assert) Mike. |
November 01, 2002 Re: Should This Be Legal? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis |
Russ Lewis wrote:
> Burton Radons wrote:
>
>
>>Russ Lewis wrote:
>>
>>>I was doing a search through an array, and I wanted to assert that I
>>>definitely would find what I was looking for in the array. So I coded
>>>this, which crashed DLI 0.1.1:
>>>
>>>for(int foo=0; assert(foo<array.length); foo++)
>>> if(do_test(array[foo]))
>>> do_stuff();
>>>
>>>Is this be legal? If not, why not?
>>
>>assert isn't an expression, so no. This assertion is redundant anyway -
>>in normal builds the array[foo] will do a bounds check, in release
>>builds neither will be checked.
>
>
> ACK!!!! I forgot the key thing in the whole code!
>
> There was supposed to be a break if you match the test:
>
> for(int foo=0; assert(foo<array.length); foo++)
> if(do_test(array[foo]))
> {
> do_stuff();
> break;
> }
>
> Does that make more sense now?
I understood what you meant. It turns out assert is an expression - I assume to allow chaining with comma - but it's still incorrect here as it returns void. I throw an error on the occasion now.
I don't think it's a common enough form to have it in the language.
|
Copyright © 1999-2021 by the D Language Foundation