www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - Remove `default` as a keyword

reply Ogion <ogion.art gmail.com> writes:
In D, `default` is reserved as a keyword, but the only case (no 
pun intended) where it is used is the `switch` statement. It 
could be substituted with `else`:

```D
bool isEven(int x) {
     // wish there was an easier way to do this...
     switch (x) {
         case 2, 4, 6, 8:
             return true;
         else:
             return false;
     }
}
```
Removing this keyword will allow users to freely use the word 
`default` for naming things.
Mar 10
next sibling parent Dejan Lekic <dejan.lekic gmail.com> writes:
On Tuesday, 10 March 2026 at 11:23:49 UTC, Ogion wrote:
 Removing this keyword will allow users to freely use the word 
 `default` for naming things.
You can argue the same for `version`, right? :)
Mar 10
prev sibling next sibling parent reply Dennis <dkorpel gmail.com> writes:
On Tuesday, 10 March 2026 at 11:23:49 UTC, Ogion wrote:
 Removing this keyword will allow users to freely use the word 
 `default` for naming things.
I often see problems with `in`, `out`, and `version` being reserved in D when translating code from other languages, but `default` is reserved as a keyword for switch statements in C, to it being reserved. For D to deviate from common switch syntax would make it harder to learn. Also consider that `goto default;` then needs to be replaced with `goto else;` which can be confusing when it's inside an if-statement, since it might look like it would go to the `else` statements from that `if` instead of the `switch`.
Mar 10
next sibling parent Serg Gini <kornburn yandex.ru> writes:
On Tuesday, 10 March 2026 at 13:52:32 UTC, Dennis wrote:
 I often see problems with `in`, `out`, and `version` being 
 reserved in D when translating code from other languages, but
Yes, in/out are popular names for function arguments. I had some issues with C lib that used them :(
Mar 10
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Tuesday, 10 March 2026 at 13:52:32 UTC, Dennis wrote:
 Also consider that `goto default;` then needs to be replaced 
 with `goto else;` which can be confusing when it's inside an 
 if-statement, since it might look like it would go to the 
 `else` statements from that `if` instead of the `switch`.
What if we made `default` reserved as a label, but allowed as a normal identifier in other contexts? This would preserve the existing behavior of `default:` and `goto default;`.
Mar 10
parent Atila Neves <atila.neves gmail.com> writes:
On Tuesday, 10 March 2026 at 16:55:00 UTC, Paul Backus wrote:
 On Tuesday, 10 March 2026 at 13:52:32 UTC, Dennis wrote:
 Also consider that `goto default;` then needs to be replaced 
 with `goto else;` which can be confusing when it's inside an 
 if-statement, since it might look like it would go to the 
 `else` statements from that `if` instead of the `switch`.
What if we made `default` reserved as a label, but allowed as a normal identifier in other contexts? This would preserve the existing behavior of `default:` and `goto default;`.
I think this would be a better proposal, but I'm not sure it's worth the effort.
Mar 11
prev sibling next sibling parent user1234 <user1234 12.de> writes:
On Tuesday, 10 March 2026 at 11:23:49 UTC, Ogion wrote:
 In D, `default` is reserved as a keyword, but the only case (no 
 pun intended) where it is used is the `switch` statement. It 
 could be substituted with `else`:

 ```D
 bool isEven(int x) {
     // wish there was an easier way to do this...
     switch (x) {
         case 2, 4, 6, 8:
             return true;
         else:
             return false;
     }
 }
 ```
 Removing this keyword will allow users to freely use the word 
 `default` for naming things.
Yes but the default-case is not necessarily at the bottom. That would be strange to have ```d switch (a) { else : break; case 0 : break; } ``` Now to be honest I've never seen a default-case not declared at the bottom. Then there's another problem. The D switch allows random statement within the body, including else-statements: ```d switch (x) { if (true) default: break; else {} } ``` still possible to change the keyword but this affects a bit the parser as you have to figure out if `else` is for the `if` or for the default-case.
Mar 10
prev sibling next sibling parent Kagamin <spam here.lot> writes:
I saw a proposal for a loop fallthrough handler
```d
foreach(a;b)
{
	if(found)return 0;
}
default
{
	puts("not found");
}
```
I think it was `else`, but that's ambiguous, `default` might be 
more idiomatic.
Mar 11
prev sibling parent reply Kagamin <spam here.lot> writes:
```d
bool isEven(int x) {
     // wish there was an easier way to do this...
     switch (x) {
         case 2, 4, 6, 8:
             return true;
         case:
             return false;
     }
}
```
Mar 12
parent reply user1234 <user1234 12.de> writes:
On Thursday, 12 March 2026 at 13:20:30 UTC, Kagamin wrote:
 ```d
 bool isEven(int x) {
     // wish there was an easier way to do this...
     switch (x) {
         case 2, 4, 6, 8:
             return true;
         case:
             return false;
     }
 }
 ```
yes, that's a bit better toward the previous comment about ordering of the default-case.
Mar 12
parent reply Dennis <dkorpel gmail.com> writes:
On Thursday, 12 March 2026 at 14:09:27 UTC, user1234 wrote:
 yes, that's a bit better toward the previous comment about 
 ordering of the default-case.
How do you distinguish `goto case;` and `goto default;` then?
Mar 12
parent user1234 <user1234 12.de> writes:
On Thursday, 12 March 2026 at 14:53:02 UTC, Dennis wrote:
 On Thursday, 12 March 2026 at 14:09:27 UTC, user1234 wrote:
 yes, that's a bit better toward the previous comment about 
 ordering of the default-case.
How do you distinguish `goto case;` and `goto default;` then?
Indeed, one step forward, on step back.
Mar 12