First Draft: Member of Operator

Richard Andrew Cattermole (Rikki) richard at cattermole.co.nz
Wed Sep 11 11:08:18 UTC 2024


The member of operator is a unified method for acquiring a member
of a context.

It rewrites into ``context.Identifier`` from ``:Identifier``.
Where the context expression is defined based upon usage. What it
requires to resolve to, depends upon the situation. Sometimes it
must be a type, others a value.

Latest:
https://gist.github.com/rikkimax/33d8cac529c1f4e4ea94b2d4b53cfcb5

Current:
https://gist.github.com/rikkimax/33d8cac529c1f4e4ea94b2d4b53cfcb5/2dba53e2a43f55bf4662036d7f18ff39ebfc3ef8

It supports returns, variable declarations, switch statements,
function calls argument-parameter matching, default
initialization of parameter, and comparison.

I have partially implemented it.

Missing in implementation is function parameter default
initialization and taking a type of a member of operator
``typeof(:Identifier)``. I was not originally going to do taking
of a type, but it is useful for sumtypes.

My motivation is for sumtypes. It allows a definition to be
compatible with library implementations as they do not require a
name.

Binary expressions are not supported, but they could be done with
a simple rewrite. A member of operator must appear as first term
in an expression and evaluate to the same type as the context.

This was pushed for by ryuukk, and thanks to IchorDev for doing
the idea thread to show a strong desire by the community!

This allows you to do inference upon a variable declaration!

```d
Enum var = :Identifier;
```

Which is equivalent to:

```d
Enum var = __traits(getMember, Enum, "Identifier");
```

In a switch statement!

```d
switch(var) {
      case :Identifier:
          break;
}
```

Function calls:

```d
void func(Enum e) {
}

func(:Identifier);
```

Even returns:

```d
Enum func() {
      return :Identifier;
}
```

It does not support binary expressions:

```d
int var = :max - 2; // Error
```


More information about the dip.development mailing list