www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - Case dot syntax

reply monkyyy <crazymonkyyy gmail.com> writes:
several languages have implicit enum handling for switch 
statements

```d
enum foo{a,b,c}

switch(somefoo){
   case .a: ...
```

add that for enums

---

for user types, require the types define an enum `opGetTypes` 
thats an array of string and an `opCastDot`

```d
struct nullable(T){
   T get;
   bool isnull;
   enum opGetTypes=["null","value"];
   ref T opCastDot(string s:"value")()=>get;
   void opCastDot(string s:"null")(){}
}
```

this will enable

```d
final switch(somenullable){
   case .value: value.writeln; break;
   case .null: "error".writeln;
}
```

Making rikkis recent dip work for one then one case(I can easily 
imagine this working for sumtypes); and matching some other 
languages syntax.
Feb 26
next sibling parent Richard (Rikki) Andrew Cattermole <richard cattermole.co.nz> writes:
Dot expressions like this, already exist and refer to module 
level symbols.
Feb 26
prev sibling next sibling parent reply Bradley Chatha <sealabjaster gmail.com> writes:
On Thursday, 26 February 2026 at 21:29:39 UTC, monkyyy wrote:
 ...
We also have the `with` statement: ```d enum SomeFoo { a, b, c } SomeFoo foo = ...; final switch(foo) with(SomeFoo) // or if you're extra: with(typeof(foo)) { case a: case b: case c: ... } ```
Mar 01
parent Juraj <junk vec4.xyz> writes:
On Sunday, 1 March 2026 at 08:56:03 UTC, Bradley Chatha wrote:
 On Thursday, 26 February 2026 at 21:29:39 UTC, monkyyy wrote:
 ...
We also have the `with` statement: ```d enum SomeFoo { a, b, c } SomeFoo foo = ...; final switch(foo) with(SomeFoo) // or if you're extra: with(typeof(foo)) { case a: case b: case c: ... } ```
This is always proposed, but `with` will now by active for the whole switch scope, I never successfully adopted this pattern in my codebase, also `switch` statement is not the only usecase for this feature. Imo `.` is the most ergonomic, there is an implementation using `$.` but the is hard on the eyes. I sadly did not found any alternative that did not "suck" in some way or another. In an ideal world, I would repurpose dot expression to something like `::`/`:`.
Mar 01
prev sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 2/26/2026 1:29 PM, monkyyy wrote:
 several languages have implicit enum handling for switch statements
 
 ```d
 enum foo{a,b,c}
 
 switch(somefoo){
    case .a: ...
 ```
I use this: ```d enum foo{a,b,c}; with (foo) switch (somefoo) { case a: ... ``
Mar 06
parent monkyyy <crazymonkyyy gmail.com> writes:
On Friday, 6 March 2026 at 20:38:59 UTC, Walter Bright wrote:
 On 2/26/2026 1:29 PM, monkyyy wrote:
 several languages have implicit enum handling for switch 
 statements
 
 ```d
 enum foo{a,b,c}
 
 switch(somefoo){
    case .a: ...
 ```
I use this: ```d enum foo{a,b,c}; with (foo) switch (somefoo) { case a: ... ``
Which does not do anything for non-enums; explicitly rikkis dip and my 2nd example is about nullable. By pushing it into switch Im suggesting it can have a unified syntax for enum, nullable and sumtype.
Mar 09