Thread overview
Did you know; we need this operator!
Sep 16
Kagamin
Sep 16
Kagamin
Sep 18
Kagamin
September 13

I have been looking at this first draft by Rikki for a few days now. We have discussed it many times before:

Finally we have this. Because it is the best way to solve the example below!

alias DaysoftheWeekinEnglish D;

void main()
{
  // with (Days)
  auto days = [
      "Pazar"     : D.Sunday,
      "Pazartesi" : D.Monday,
      "Salı"      : D.Tuesday,
      "Çarşamba"  : D.Wednesday,
      "Perşembe"  : D.Thursday,
      "Cuma"      : D.Friday,
      "Cumartesi" : D.Saturday
  ];
  assert(days["Cuma"] == DaysoftheWeekinEnglish.Friday);
  assert(days["Pazar"] == D.Sunday);

In this example, if you open the with() scope, everything works inside the scope and the object is created correctly. However, outside the scope, the object does not actually exist!

However, Rikki wrote that the proposed draft for this example would not be a solution. However, with() looks nice but should be used with caution.

SDB@79

September 14

On Friday, 13 September 2024 at 18:49:06 UTC, Salih Dincer wrote:

>
alias DaysoftheWeekinEnglish D;

void main()
{
  // with (Days)
  auto days = [
      "Pazar"     : D.Sunday,
      "Pazartesi" : D.Monday,
      "Salı"      : D.Tuesday,
      "Çarşamba"  : D.Wednesday,
      "Perşembe"  : D.Thursday,
      "Cuma"      : D.Friday,
      "Cumartesi" : D.Saturday
  ];
  assert(days["Cuma"] == DaysoftheWeekinEnglish.Friday);
  assert(days["Pazar"] == D.Sunday);

In this example, if you open the with() scope, everything works inside the scope and the object is created correctly. However, outside the scope, the object does not actually exist!

This could also be solved by allowing with() to be used as an expression, not just a statement:

void main()
{
    auto days = with (Days) [
        "Pazar" : Sunday,
        // ...
    ];
}
September 16

As for scope extension, C# has extended scope statements:
This is normal scoped statement:

with(Days){ ... }

This is extended scoped statement:

{
with Days;
...
}

September 16

Lol, thank you, markdown.

with(Days){ ... }
{
	with Days;
	...
}
September 16

On Monday, 16 September 2024 at 08:47:51 UTC, Kagamin wrote:

>

As for scope extension, C# has extended scope statements:

This is normal scoped statement:

with(Days){ ... }

This is extended scoped statement:

{
	with Days;
	...
}

Moving on to C++, there is no direct operator to shorten enum member names. However, there are several methods to make enum members more readable. You can shorten long enum names using typedef or using:

#include <iostream>
using namespace std;

//* <--For other solution remove the first character
enum Days
{
  Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};

typedef Days Day;

/*/
//  Namespace Usage:
//  You can place enum members inside
//  a namespace to use shorter names:


namespace Days
{
  enum Day
  {
    Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
  };
}

using namespace Days;//*/

int main()
{
    Day today = Sunday;

    cout << "Hello " << today;

    return 0;
}

From these examples, we see that C++ is proactive in solving the problem.

SDB@79

September 18

On Saturday, 14 September 2024 at 01:01:06 UTC, Paul Backus wrote:

>

This could also be solved by allowing with() to be used as an expression, not just a statement:

In fact, if the D parser is not sufficient, you can make your own parser using a simple regular expression! Dconf'24 brought me such a perspective:

import std.regex, std.stdio;

enum Days
{
  Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};

void main()
{
  auto jsonStr = `{
      "Pazar": Sunday,
      "Pazartesi": Monday,
      "Salı": Tuesday,
      "Çarşamba": Wednesday,
      "Perşembe": Thursday,
      "Cuma": Friday,
      "Cumartesi": Saturday,
  }`;

  Days[string] Item;

  enum regex = `"(\w+)": (\w+),`;
  foreach (matches; jsonStr.matchAll(regex))
  {
    string key = matches.captures[1];
    string value = matches.captures[2];
    writefln("\"%s\": %s", key, value);
    Item[key] = value.indexOf!Days;
  }
  Item.writeln;
}

auto indexOf(E)(string value)
{
  import std.conv : to;

  E count;
  while (count < count.max)
  {
    if (count.to!string == value) break;
    ++count;
  }
  return count;
}

Prints:

/*
"Pazar": Sunday
"Pazartesi": Monday
"Salı": Tuesday
"Çarşamba": Wednesday
"Perşembe": Thursday
"Cuma": Friday
"Cumartesi": Saturday
["Cumartesi":Saturday, "Pazartesi":Monday, "Pazar":Sunday, "Salı":Tuesday, "Çarşamba":Wednesday, "Perşembe":Thursday, "Cuma":Friday]

Process finished.
*/

SDB@79

September 18

On Monday, 16 September 2024 at 08:49:39 UTC, Kagamin wrote:

>

Lol, thank you, markdown.

with(Days){ ... }
{
	with Days;
	...
}

By far the most elegant solution. Especially as in most cases, the extra scope is either already there or wouldn’t be needed. In a switch statement, it can even go before the first case:

switch (day)
{
    with Days;
case Monday:
    …;
    break;
case Tuesday:
    …;
    break;
}

Still, I think for switch in particular, if a label is a qualified identifier QI and doesn’t resolve, it should try typeof(SwitchedExpression).QI. Solves most cases and is flexible.

September 18

If you have an extra scope to apply, it already works:

with(Days)
switch(day)
{
case Monday:
     …;
     break;
case Tuesday:
     …;
     break;
}
September 18

On Wednesday, 18 September 2024 at 16:30:41 UTC, Kagamin wrote:

>

If you have an extra scope to apply, it already works:

with(Days)
switch(day)
{
case Monday:
     …;
     break;
case Tuesday:
     …;
     break;
}

Is there a with() in C#? It seems like there isn't one; record has a with!

SDB@79

September 19

On Saturday, 14 September 2024 at 01:01:06 UTC, Paul Backus wrote:

>

On Friday, 13 September 2024 at 18:49:06 UTC, Salih Dincer wrote:

>

[...]

This could also be solved by allowing with() to be used as an expression, not just a statement:

void main()
{
    auto days = with (Days) [
        "Pazar" : Sunday,
        // ...
    ];
}

https://github.com/dlang/dmd/pull/13776