digitalmars.D - Did you know; we need this operator!
- Salih Dincer (35/35) Sep 13 I have been looking at [this first draft by
- Paul Backus (12/32) Sep 13 This could also be solved by allowing `with()` to be used as an
- Salih Dincer (61/63) Sep 18 In fact, if the D parser is not sufficient, you can make your own
- max haughton (2/16) Sep 19 https://github.com/dlang/dmd/pull/13776
- Kagamin (12/12) Sep 16 As for scope extension, C# has extended scope statements:
- Kagamin (10/10) Sep 16 Lol, thank you, markdown.
- Quirin Schroll (20/30) Sep 18 By far the most elegant solution. Especially as in most cases,
- Kagamin (13/13) Sep 18 If you have an extra scope to apply, it already works:
- Salih Dincer (4/17) Sep 18 Is there a with() in C#? It seems like there isn't one; record
- Salih Dincer (36/48) Sep 16 Moving on to C++, there is no direct operator to shorten enum
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
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
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
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:https://github.com/dlang/dmd/pull/13776[...]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 19
This is normal scoped statement: --- with(Days){ ... } --- This is extended scoped statement: --- { with Days; ... } ---
Sep 16
Lol, thank you, markdown. ``` with(Days){ ... } ``` ``` { with Days; ... } ```
Sep 16
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
If you have an extra scope to apply, it already works: ```d with(Days) switch(day) { case Monday: …; break; case Tuesday: …; break; } ```
Sep 18
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
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