www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Did you know; we need this operator!

reply Salih Dincer <salihdb hotmail.com> writes:
I have been looking at [this first draft by 
Rikki](https://gist.github.com/rikkimax/33d8cac529c1f4e4ea94b2d4b53cfcb5) for a
few days now. We have discussed it many times before:

* [First Draft: Member of 
Operator](https://forum.dlang.org/post/xtmwtmkhmufpzeyrsxsb forum.dlang.org)
* [Type Inference for Struct/Enum 
Literals](https://forum.dlang.org/post/zbugncpaooowjsxldzue forum.dlang.org)
* [The enum type inference problem is easily solved with 
identifier 
types](https://forum.dlang.org/post/tvnnoujdmaedgertbdes forum.dlang.org)

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

```d
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
Sep 13
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Friday, 13 September 2024 at 18:49:06 UTC, Salih Dincer wrote:
 ```d
 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: ```d void main() { auto days = with (Days) [ "Pazar" : Sunday, // ... ]; } ```
Sep 13
next sibling parent Salih Dincer <salihdb hotmail.com> writes:
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: ```D 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:*** ```D /* "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
Sep 18
prev sibling parent max haughton <maxhaton gmail.com> writes:
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: ```d void main() { auto days = with (Days) [ "Pazar" : Sunday, // ... ]; } ```
https://github.com/dlang/dmd/pull/13776
Sep 19
prev sibling parent reply Kagamin <spam here.lot> writes:

This is normal scoped statement:
---
with(Days){ ... }
---

This is extended scoped statement:
---
{
	with Days;
	...
}
---
Sep 16
next sibling parent reply Kagamin <spam here.lot> writes:
Lol, thank you, markdown.

```
with(Days){ ... }
```

```
{
	with Days;
	...
}
```
Sep 16
parent reply Quirin Schroll <qs.il.paperinik gmail.com> writes:
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: ```d 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.
Sep 18
parent reply Kagamin <spam here.lot> writes:
If you have an extra scope to apply, it already works:
```d
with(Days)
switch(day)
{
case Monday:
      …;
      break;
case Tuesday:
      …;
      break;
}
```
Sep 18
parent Salih Dincer <salihdb hotmail.com> writes:
On Wednesday, 18 September 2024 at 16:30:41 UTC, Kagamin wrote:
 If you have an extra scope to apply, it already works:
 ```d
 with(Days)
 switch(day)
 {
 case Monday:
      …;
      break;
 case Tuesday:
      …;
      break;
 }
 ```
has a with! SDB 79
Sep 18
prev sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Monday, 16 September 2024 at 08:47:51 UTC, Kagamin wrote:


 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: ```CPP #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
Sep 16