New Issue: Should we use a special character to decorate special methods?

19050, "e-kayrakli", "Should we use a special character to decorate special methods?", "2022-01-19T18:50:51Z"

This is related to What, if anything, should we do about special method names? · Issue #19038 · chapel-lang/chapel · GitHub.

We might want to have a different "namespace" for special methods. See the issue above for examples of such methods. But consider a foo method that has special meaning. We might want to have such a method (or operator?) while not precluding the user from defining their own foo with a different meaning. Can we name foo something else to achieve that?

  • Python way of doing it would be to name it __foo__
  • Somewhat conventional way of doing that in Chapel could be prefixing it with chpl_ or chpl__
  • Another ideas that are thrown around also include prefix/suffixing _ to the method name.

I personally do not like adding special meaning to anything with _. The underscore is a valid character for identifiers and should be used by the user as they see fit. There can be some coding conventions that are not enforced by the compiler that rely on underscores, but what we are looking for here is a stronger guarantee and I don't think underscore can give us that.

If we want to separate namespaces by some naming scheme, I think we should use a more special character. That special character should also appear somehow at the call site. I have two favorites in my mind today: @ and ~

  • @ is too special that it cannot appear anywhere in the language as far as I know. I find it a bit too... much visually. We can call these methods "at-methods".
  • ~ is binary not. But I think contexts are clearly different and this shouldn't cause any ambiguity for the parser or confusion for the user. The only potential confusion is with C++ destructors. We can call these special methods "tilde-methods" or "squiggly-methods".
  • (I had also thought about ::, but that's just way too confusing with C++. I mean we might as well use -> if we are doing that? This is definitely not what I am proposing :slight_smile: )

As this is a little bit about looks, here's a sketch of how these could look like:

record R {
  proc @foo() {

  }
}

var r = new R();
r.@foo();

or

record R {
  proc ~foo() {

  }
}

var r = new R();
r.~foo();

Potentially far-fetched idea about the call site

Maybe we don't need . at the call site for these? And the special symbol we choose is the operator that connects the method symbol to the receiver symbol?

r@foo();

or

r~foo();