www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - proposal: should assert behave like static assert if its possible?

reply dennis luehring <dl.soluz gmx.net> writes:
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
Jan 19 2008
parent reply torhu <no spam.invalid> writes:
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.
Jan 19 2008
parent reply Walter Bright <newshound1 digitalmars.com> writes:
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.
Jan 19 2008
next sibling parent BCS <ao pathlink.com> writes:
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).
Jan 19 2008
prev sibling next sibling parent dennis luehring <dl.soluz gmx.net> writes:
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
Jan 20 2008
prev sibling parent dennis luehring <dl.soluz gmx.net> writes:
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
Jan 21 2008