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
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!
Enum var = :Identifier;
Which is equivalent to:
Enum var = __traits(getMember, Enum, "Identifier");
In a switch statement!
switch(var) {
case :Identifier:
break;
}
Function calls:
void func(Enum e) {
}
func(:Identifier);
Even returns:
Enum func() {
return :Identifier;
}
It does not support binary expressions:
int var = :max - 2; // Error