www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - static if syntax

reply Steve Teale <steve.teale britseyeview.com> writes:
If I write:

static if (cond1)
{
 ...
}
else static if (cond2)
{
 ...
}
else
{
 ...
}

is the block after the final else 'static'? Would it be better if the 'static'
before the whole sequence of tests applied throughout rather than having to be
explicitly restated in some places, but not in others?

Steve
Jun 04 2009
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Steve Teale:

is the block after the final else 'static'?<
Yes, it is.
Would it be better if the 'static' before the whole sequence of tests applied
throughout rather than having to be explicitly restated in some places, but not
in others?<
Few times I have forgotten to put a "static" in such chain of static ifs (so I may appreciate a kind of static switch) but in practice I think your idea will lead to other bugs. So it may be better to not change the current design. Bye, bearophile
Jun 04 2009
prev sibling next sibling parent BCS <ao pathlink.com> writes:
Reply to Steve,

 If I write:
 
 static if (cond1)
 {
 ...
 }
 else static if (cond2)
 {
 ...
 }
 else
 {
 ...
 }
 is the block after the final else 'static'? Would it be better if the
 'static' before the whole sequence of tests applied throughout rather
 than having to be explicitly restated in some places, but not in
 others?
 
 Steve
 
Whatever is used, both of these (and other variations) should be cleanly and consistently representable. static if(foo) { static if(bar) {} else {} } else { static if(baz) {} else {} } static if(foo) { if(bar) {} else {} } else { if(baz) {} else {} }
Jun 04 2009
prev sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Steve Teale wrote:
 If I write:
 
 static if (cond1)
 {
  ...
 }
 else static if (cond2)
 {
  ...
 }
 else
 {
  ...
 }
 
 is the block after the final else 'static'? Would it be better if the 'static'
before the whole sequence of tests applied throughout rather than having to be
explicitly restated in some places, but not in others?
 
 Steve
I don't think so. The problem is that there's no such thing as "chained ifs" in the language. What you're actually looking at is this: static if (cond1) { ... } else { static if (cond2) { ... } else { ... } } Once you realise that there's nothing special about either "else if" or "else static if", it makes perfect sense.
Jun 04 2009
parent reply Steve Teale <steve.teale britseyeview.com> writes:
Daniel Keep Wrote:

 
 
 Steve Teale wrote:
 If I write:
 
 static if (cond1)
 {
  ...
 }
 else static if (cond2)
 {
  ...
 }
 else
 {
  ...
 }
 
 is the block after the final else 'static'? Would it be better if the 'static'
before the whole sequence of tests applied throughout rather than having to be
explicitly restated in some places, but not in others?
 
 Steve
I don't think so. The problem is that there's no such thing as "chained ifs" in the language. What you're actually looking at is this: static if (cond1) { ... } else { static if (cond2) { ... } else { ... } } Once you realise that there's nothing special about either "else if" or "else static if", it makes perfect sense.
Daniel, So I have to write static if (cond1) { } else static if (cond2) { } else static if (true) { // for the default alternative? } Looks a bit strange Steve
Jun 04 2009
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Steve Teale wrote:
 ...
 
 Daniel,
 
 So I have to write
 
 static if (cond1)
 {
 }
 else static if (cond2)
 {
 }
 else static if (true)
 {
    // for the default alternative?
 }
No.
 Looks a bit strange
 Steve
That's because it's wrong. static if (cond) ... else ... is a single structure. The branch is chosen at compile-time. It doesn't make sense for the else to somehow not be part of the static if.
Jun 04 2009
parent Steve Teale <steve.teale britseyeview.com> writes:
Daniel Keep Wrote:


 That's because it's wrong.
 
 static if (cond) ... else ... is a single structure.  The branch is
 chosen at compile-time.  It doesn't make sense for the else to somehow
 not be part of the static if.
Daniel, Sorry, I'm there now. I agree completely that otherwise it does not make sense, but what you say now was not the impression I got from the previous answers. I had unfortunately, been looking for the appropriate documentation in all the wrong places. A search in the html files comes up with version.html, which did not seem to be that relevant. Perhaps the file name should reflect the html title, and be conditional-compilation.html. Thanks Steve
Jun 05 2009