Defining a new operator that performs member access

37 Views Asked by At

I'm trying to create a custom operator that is a synonym for the dot (member access) operator, such that a>>=b results in a.b.

My first attempt does not compile

operator >>= left 19 = (left, right) => {
    return #`${left}.${right}]`;
};

If i change . by && in the return statement, then it does compile.

Since the previous approach did not work out, i tried using computed member access:

operator >>= left 19 = (left, right) => {
  let r = right.name.token.value; // string representation of an identifier
  let dummy = #`dummy`.get(0);
  return #`${left} [${fromStringLiteral(dummy, r)}]`;
};

This approach compiles for simple expressions like a>>=b, but not for a>>=b.then(res => ...), because right is now a call expression: b.then(...), which i don't understand since i chose a precedence of 19 for my custom operator, which is also the precedence of [] hence conceptually this means that my expression should be equivalent to (a>>b).then(res => ...) or am i wrong?

0

There are 0 best solutions below