First Draft: Member of Operator

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Fri Sep 13 08:41:11 UTC 2024


On 13/09/2024 6:39 AM, Salih Dincer wrote:
> On Wednesday, 11 September 2024 at 11:08:18 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> The member of operator is a unified method for acquiring a member
>> of a context.
>>
>> It rewrites into ``context.Identifier`` from ``:Identifier``.
> 
> It's used very common this sign (colon ```:```)  wouldn't cause 
> confusion or pending trouble? In fact The symbol in draft resembles the 
> assignment operator in J language. The : symbol is used in both switches 
> (to the right of the ```case```) and enums. The ```: symbol``` in enums 
> is typically used to specify the underlying data type of the enum. This 
> determines the data type in which the enum constants will be stored. For 
> example, you can have an enum based on int, byte, short, or string.
> 
> For instance in D, we can use the ```: symbol``` when defining an enum 
> like this:
> 
> ```d
> enum Days : byte
> {
>      Sunday = 1 , Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
> }
> ```
> In this case, using enums on the left and right means mental confusion. 
> Similarly, in cases, on the right and left at the same time...

See my follow up post, due to first term only, this does not conflict 
with other language features.

> **Last question:** Can we get rid of the parentheses like in the example 
> below? Just like when using ```with(E)```
> 
> ```d
> T mul(uint e = 1, T)(T value) if (e < 3)
> {
>      static if (e == E.Positive)
>      {
>          return 1 * value;
>      }
>      else static if (e == E.Superposition)
>      {
>          return 2 * value;
>      }
>      return 0;
> }
> 
> enum E
> {
>      Negative, Positive, Superposition
> }
> 
> import std.stdio;
> void main()
> {
>      4.mul!(E.Positive).writeln;
>    //4.mul!:Positive.writeln;
>      with (E) 4.mul!Superposition.writeln;
>      4.mul!2.writeln;
> }
> ```
> 
> SDB at 79

The template parameter would need to be of type ``E`` not ``uint`` to 
provide a context.

Its implemented using two tokens ``:`` and ``Identifier``. So knee jerk 
reaction is no, even if it was supported for templates.

Hmm, grammar changes look ok, and I do need it to Type for casting of 
sumtypes... so maybe this is a good change even if I don't like it.

```diff
TemplateSingleArgument:
+    MemberOfOperator

Type:
+    MemberOfOperator
```


More information about the dip.development mailing list