digitalmars.D - static if syntax
- Steve Teale (15/15) Jun 04 2009 If I write:
- bearophile (5/7) Jun 04 2009 Few times I have forgotten to put a "static" in such chain of static ifs...
- BCS (7/28) Jun 04 2009 Whatever is used, both of these (and other variations) should be cleanly...
- Daniel Keep (20/38) Jun 04 2009 I don't think so. The problem is that there's no such thing as "chained
- Steve Teale (15/58) Jun 04 2009 Daniel,
- Daniel Keep (6/24) Jun 04 2009 That's because it's wrong.
- Steve Teale (7/12) Jun 05 2009 Daniel,
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
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
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? SteveWhatever 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
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? SteveI 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
Daniel Keep Wrote:Steve Teale wrote: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 SteveIf 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? SteveI 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
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 SteveThat'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
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