Skip to content

Instantly share code, notes, and snippets.

@rikkimax
Last active February 8, 2024 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikkimax/9e02ad538d94615d76d869070f7fd65f to your computer and use it in GitHub Desktop.
Save rikkimax/9e02ad538d94615d76d869070f7fd65f to your computer and use it in GitHub Desktop.

PR: dlang/dmd#16161

Member Of Operator

The member of operator, is an operator that operates on a contextual type with respect to a given statement, declaration or expression.

It may appear as the first term in an expression, then it may be followed with binary and dot expressions.

The syntax of the operator is ':' Identifier.

It is commonly implemented by doing a rewrite to: context.Identifier. Where the context is a type which was provided by the usage syntax.

Validation

The type that the member of operator results in is the same as the one it is in context of.

If it does not match, it will error.

Valid Statements and Declarations

  • Return expressions The compiler rewrites return :Identifier; as return typeof(return).Identifier;.
  • Variable declarations Type qualifiers may not appear as the variable type, there must be a concrete type. It can be thought of as the type on the variable as having been aliased with the alias applying to the variable type and as the context. Type var = :Identifier; would internally be rewritten as __Alias var = __Alias.Identifier;.
  • Switch statements The expression used by the switch statement, will need to be aliased as per variable declarations. So
    switch(expr) {
        case :Identifier:
            break;
    }
    would be rewritten as
    alias __Alias = typeof(expr);
    switch(expr) {
        case __Alias.Identifier:
            break;
    }
  • Function calls During parameter to argument matching, a check to see if the typeof(param).Identifier is possible for func(:Identifier).
  • Function parameter default initialization It must support the default initialization of a parameter. void func(Enum e = :Start).
  • Comparison The left hand side type of a comparison is used as the context for the right hand side e == :Start. This may require an intermediary variable to get the type of, prior to the comparison.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment