Thread overview
proposal: should assert behave like static assert if its possible?
Jan 19, 2008
dennis luehring
Jan 20, 2008
torhu
Jan 20, 2008
Walter Bright
Jan 20, 2008
BCS
Jan 20, 2008
dennis luehring
Jan 21, 2008
dennis luehring
January 19, 2008
hi group,

this is one of my function needed to talk to an siemens plc(sps)

int byte_bit_to_int( int byte_, int bit_ )
{
 // if bit_ is compiletime value
   static assert( bit_ <= 7 );

 // if bit_ is runtime value
   assert( bit_ <= 7 );

 return byte_*8+bit_;
}

and my question is how can i make the assert as save as possible
at compile- and runtime

my question for walter is:

can't assert behave like static assert if the value/condition is available compiletime

for example:

int x = byte_bit_to_int( 7, 9 ); // an static assert could do the check

int x = byte_bit_to_int( 7, random(8) ); // the normal assert is needed

ciao dennis
January 20, 2008
dennis luehring wrote:
> my question for walter is:
> 
> can't assert behave like static assert if the value/condition is available compiletime
> 
> for example:
> 
> int x = byte_bit_to_int( 7, 9 ); // an static assert could do the check
> 
> int x = byte_bit_to_int( 7, random(8) ); // the normal assert is needed

The problem is that sometimes you write things like assert(0), and need it to trigger at runtime, not compile time.  So I think there would still be a way to do that.
January 20, 2008
torhu wrote:
> dennis luehring wrote:
>> my question for walter is:
>>
>> can't assert behave like static assert if the value/condition is available compiletime
>>
>> for example:
>>
>> int x = byte_bit_to_int( 7, 9 ); // an static assert could do the check
>>
>> int x = byte_bit_to_int( 7, random(8) ); // the normal assert is needed
> 
> The problem is that sometimes you write things like assert(0), and need it to trigger at runtime, not compile time.  So I think there would still be a way to do that.

You're right (it's runtime flow of control sensitive), and that's why it doesn't trigger at compile time.
January 20, 2008
Reply to Walter,

> torhu wrote:
> 
>> dennis luehring wrote:
>> 
>>> my question for walter is:
>>> 
>>> can't assert behave like static assert if the value/condition is
>>> available compiletime
>>> 
>>> for example:
>>> 
>>> int x = byte_bit_to_int( 7, 9 ); // an static assert could do the
>>> check
>>> 
>>> int x = byte_bit_to_int( 7, random(8) ); // the normal assert is
>>> needed
>>> 
>> The problem is that sometimes you write things like assert(0), and
>> need it to trigger at runtime, not compile time.  So I think there
>> would still be a way to do that.
>> 
> You're right (it's runtime flow of control sensitive), and that's why
> it doesn't trigger at compile time.
> 

How about have a flag that spits out a list of these as warnings? For sanity's sake it might skip assert(false). 


January 20, 2008
Walter Bright schrieb:
> torhu wrote:
>> dennis luehring wrote:
>>> my question for walter is:
>>>
>>> can't assert behave like static assert if the value/condition is available compiletime
>>>
>>> for example:
>>>
>>> int x = byte_bit_to_int( 7, 9 ); // an static assert could do the check
>>>
>>> int x = byte_bit_to_int( 7, random(8) ); // the normal assert is needed
>>
>> The problem is that sometimes you write things like assert(0), and need it to trigger at runtime, not compile time.  So I think there would still be a way to do that.
> 
> You're right (it's runtime flow of control sensitive), and that's why it doesn't trigger at compile time.

an idea:

deprecate assert( static false ) constructs in one of the next releases
programmers need to replace it with assert() then

and later you can just activate this "assert behaves likes static assert if possible" feature - over night :-)

i think this feature will help a lot to get more stable programs out of D code

ciao dennis




January 21, 2008
Walter Bright schrieb:
> torhu wrote:
>> dennis luehring wrote:
>>> my question for walter is:
>>>
>>> can't assert behave like static assert if the value/condition is available compiletime
>>>
>>> for example:
>>>
>>> int x = byte_bit_to_int( 7, 9 ); // an static assert could do the check
>>>
>>> int x = byte_bit_to_int( 7, random(8) ); // the normal assert is needed
>>
>> The problem is that sometimes you write things like assert(0), and need it to trigger at runtime, not compile time.  So I think there would still be a way to do that.
> 
> You're right (it's runtime flow of control sensitive), and that's why it doesn't trigger at compile time.

i know why it doesn't trigger at compiletime

my question is - why can't you make it trigger at compiletime
if the condition is false at compiletime

we (just) need an replacement for assert(false) or assert(0) constructs, right?

why don't replace this assert(false) constructs with just assert()

ciao dennis