May 14, 2021

On Friday, 14 May 2021 at 08:37:19 UTC, Mike Parker wrote:

>

On Friday, 14 May 2021 at 08:24:46 UTC, Paulo Pinto wrote:
ines.md#es78-dont-rely-on-implicit-fallthrough-in-switch-statements

>

Additionally, since C++17, there is the fallthrough attribute to be explicit that the C behaviour is actually intended.

https://en.cppreference.com/w/cpp/language/attributes/fallthrough

Man that's ugly. I like our goto case better.

In C gcc uses comments to silence the warning.

case 5: ...
/* falltrough */
case 6: ...
May 14, 2021
On Friday, 14 May 2021 at 08:37:19 UTC, Mike Parker wrote:
> On Friday, 14 May 2021 at 08:24:46 UTC, Paulo Pinto wrote:
> ines.md#es78-dont-rely-on-implicit-fallthrough-in-switch-statements
>>
>> Additionally, since C++17, there is the fallthrough attribute to be explicit that the C behaviour is actually intended.
>>
>> https://en.cppreference.com/w/cpp/language/attributes/fallthrough
>
> Man that's ugly. I like our `goto case` better.

Lol is this real life? It's starting to look like an alien language
May 14, 2021

On Thursday, 13 May 2021 at 14:25:53 UTC, rempas wrote:

>

Exactly! We can mix things up a little! Or even add a new statement called "match" idk

Yes. Rather than changing switch behavior, we could add a whole different statement. Additionally, we could allow it to operate on non-constants. Basically like an if .. else if chain, but with better syntax. We could also make it more powerful, implementing pattern matching. And the compiler could rewrite match statement as the standard switch statement if it detects that all case values are known at compile time.

That was in my wishlist for D for a long time. I find both switch and if .. else if awkward, we could do better than that. But it seems we are in a minority here. Named arguments and string interpolation are far more popular and important features but we are still waiting for them.

There’s one more problem: Walter et al. always avoid adding new keywords to the language, preferring to reuse the existing ones whenever possible. Well, we could use if switch.

Since case statements inside if switch are not labels, we should probably bring them in line with statements like if: require curly braces if there’s more than one line, and parenthesis surrounding the condition.

int i = 42, a = 10, b = 20;
if switch (i) {
    case (a)
        "i == a".writeln;
    case (b) {
        "i == b".writeln;
        someFunction();
    }
    default {
        "oh no".writeln;
    }
}
May 14, 2021
On 5/14/21 12:39 PM, Ogi wrote:
> On Thursday, 13 May 2021 at 14:25:53 UTC, rempas wrote:
>> Exactly! We can mix things up a little! Or even add a new statement called "match" idk

...

> There’s one more problem: Walter et al. always avoid adding new keywords to the language, preferring to reuse the existing ones whenever possible. Well, we could use `if switch`.

`new switch` ;)
May 14, 2021

On Friday, 14 May 2021 at 19:39:09 UTC, Ogi wrote:

>

But it seems we are in a minority here. Named arguments and string interpolation are far more popular and important features but we are still waiting for them.

I actually suspect there's a non-vocal group of D users dissatisfied with the old switch, though I don't think there's consensus on what the new switch would look like.

Personally, I think C#'s switch expressions look appealing, and they are compatible with D's syntax. Here's what the basic form looks like:

void main() {
    int x = 3;
    string name = x switch {
        0 => "zero",
        1 => "one",
        2 => "two",
    };
}

Examples with pattern-matching and case guards can be found in the link.

May 14, 2021
On Friday, 14 May 2021 at 20:53:09 UTC, David Gileadi wrote:
> On 5/14/21 12:39 PM, Ogi wrote:
>> On Thursday, 13 May 2021 at 14:25:53 UTC, rempas wrote:
>>> Exactly! We can mix things up a little! Or even add a new statement called "match" idk
>
> ...
>
>> There’s one more problem: Walter et al. always avoid adding new keywords to the language, preferring to reuse the existing ones whenever possible. Well, we could use `if switch`.
>
> `new switch` ;)

`switch with out break` :P
May 14, 2021
On 5/14/21 3:40 PM, ag0aep6g wrote:
> On Friday, 14 May 2021 at 20:53:09 UTC, David Gileadi wrote:
>> On 5/14/21 12:39 PM, Ogi wrote:
>>> On Thursday, 13 May 2021 at 14:25:53 UTC, rempas wrote:
>>>> Exactly! We can mix things up a little! Or even add a new statement called "match" idk
>>
>> ...
>>
>>> There’s one more problem: Walter et al. always avoid adding new keywords to the language, preferring to reuse the existing ones whenever possible. Well, we could use `if switch`.
>>
>> `new switch` ;)
> 
> `switch with out break` :P

+1000
May 15, 2021

On Friday, 14 May 2021 at 20:53:09 UTC, David Gileadi wrote:

>

On 5/14/21 12:39 PM, Ogi wrote:

>

On Thursday, 13 May 2021 at 14:25:53 UTC, rempas wrote:

>

Exactly! We can mix things up a little! Or even add a new statement called "match" idk

...

>

There’s one more problem: Walter et al. always avoid adding new keywords to the language, preferring to reuse the existing ones whenever possible. Well, we could use if switch.

new switch ;)

static switch ;-)

May 15, 2021
On 5/14/21 6:40 PM, ag0aep6g wrote:
> On Friday, 14 May 2021 at 20:53:09 UTC, David Gileadi wrote:
>> On 5/14/21 12:39 PM, Ogi wrote:
>>> On Thursday, 13 May 2021 at 14:25:53 UTC, rempas wrote:
>>>> Exactly! We can mix things up a little! Or even add a new statement called "match" idk
>>
>> ...
>>
>>> There’s one more problem: Walter et al. always avoid adding new keywords to the language, preferring to reuse the existing ones whenever possible. Well, we could use `if switch`.
>>
>> `new switch` ;)
> 
> `switch with out break` :P

That's just fantastic.
May 21, 2021

On Friday, 14 May 2021 at 20:58:57 UTC, Dennis wrote:

>
void main() {
    int x = 3;
    string name = x switch {
        0 => "zero",
        1 => "one",
        2 => "two",
    };
}

This particular example can be emulated with this template:

template mapSwitch(alias map)
{
	import std.traits;
	alias T = KeyType!(typeof(map));
	auto mapSwitch(T value)
	{
		switch (value) {
			static foreach (key; map.keys) {
				case key: return map[key];
			}
            default: assert(0);
		}
	}
}

void main() {
	int x = 2;
	string name = x.mapSwitch!([
		0 : `zero`,
		1 : `one`,
		2 : `two`,
	]);
}

Both the keys and the values must be known at compile time though. We can’t go around this by using lambdas because D doesn’t support passing function pointers as template parameters.