Bug 1053 – Make 'static else if' or 'static if (...) {...} else if' work
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2007-03-11T13:28:00Z
Last change time
2016-10-14T00:23:03Z
Assigned to
nobody
Creator
wbaxter
Comments
Comment #0 by wbaxter — 2007-03-11T13:28:05Z
It would be nice if "else static if" could be written "static else if". Or even better just "else if".
"else static if" struck me as odd the first time I saw it, and since then I continue to mistype it as "static else if" pretty much every time I use it.
So it would be great if "static else if" just worked.
On the other hand, I'm pretty sure
static if (foo) { }
else if (bar) { }
is an error currently, so it would be nice if that were just taken to be a "static else if". There isn't a "static else {}" so why should we have to repeat the "static" on an "else if"? One "static" should be enough.
I guess this could be an issue?:
static if (foo)
if (biff)
single_statement();
else if (bar) { }
But I could live without being able to do that. Oh, and besides that it's already ambiguous with plain ifs...
if (foo)
if (biff)
single_statement();
else if (bar) { }
Exactly the same deal -- who does the else belong to?
--
As an added bonus either of these changes would make the Emacs D-mode capable of indenting static ifs with else ifs properly. That's not a sufficient reason to make the change (emacs D-mode could be made to work somehow) But the fact that cc-mode basically sees "else static if" as an aberration was the thing that finally drove me to file an enhancement request on this.
Comment #1 by fvbommel — 2007-03-12T09:10:22Z
Jarrett Billingsley wrote:
> "Bill Baxter" <[email protected]> wrote in message
> news:[email protected]...
>> Maybe perl/python/ruby/cpp/tcl were right. Else if should be a single
>> keyword. Then it wouldn't be an issue.
>
> Phh.. that's because their grammars aren't as flexible ;)
>
> But C++ doesn't have "elseif" as a single word. Maybe you're thinking of
> the "#elif" preprocessor command?
I didn't see 'c++' mentioned, only 'cpp', which is (confusingly) also a
common acronym for the C (or C++) pre-processor. So I'd guess he was
indeed thinking of #elif :).
Comment #2 by wbaxter — 2007-03-12T18:35:52Z
(For the record, there was some discussion on this on the newsgroup Frits' message above was in reply to something there about which languages use an 'elseif' keyword).
Anyway, I can see the difficulties involved in the implementation of this, but I still think it would make D better for users. In the end, it's just an enhancement request. If Walter agrees then maybe something will be done about it, if not then he'll close it.
But here's another attempt to sell the idea: I'm pretty sure there are very few C/C++/D/Java users who really think of
if () {
}
else if () {
}
else if () {
}
else () {
}
as this:
if () {
}
else {
if () {
}
else {
if () {
}
else () {
}
}
}
which is pretty much the way you have to be thinking for 'else static if' to make sense.
Comment #3 by matti.niemenmaa+dbugzilla — 2007-03-17T12:03:23Z
That _is_ the way I often think of if-else. :-P I prefer the way it currently works, as I do sometimes write:
static if
else if
And it means what I want it to mean.
Sometimes, though, I find that it's clearer to write something as:
if
else {
if
else
}
Instead of:
if
else if
else
Depending on what exactly is in the first else.
"static else if" would complicate the grammar a bit (written in about a minute, not sure if the following would be correct):
if: "if [expression] [statement] (else [statement])"
static if: "[static if [expression] [statement] (else [statement])] | [static if [expression] [statement] (static else [if-statement])]"
As opposed to the current:
if or static if: "(static) if [expression] [statement] (else [statement])"