Improve method syntax #1777
Replies: 4 comments
-
Now change But, such changes simplify code writing and readability |
Beta Was this translation helpful? Give feedback.
-
Maybe an outside view from someone just getting into touch with Carbon. This method syntax was the very first thing that struck me as a step back from C++ actually. So I'm happy to see this is a topic of discussion. Whether something is a function, a mutating method or a const method is something very fundamental, both when writing code and when reading APIs. Something that should have "first level" support by the language. In Carbon, I just get the feeling that methods (as opposed to functions) are somehow second-level citizens. You start with functions ( As a developer, I would much prefer to state the higher-level intent at this point. Ideally in some short, concise form, at an easy to spot location. Which is why something like |
Beta Was this translation helpful? Give feedback.
-
Swift is trying to add explicit self annotation to methods so it can hang attributes to self. |
Beta Was this translation helpful? Give feedback.
-
I like the idea to move self parameter inside parens instead of square brackets, it does not make sense for me to have it inside square brackets. I do not like the idea of self keyword meaning two things (receiver, and its type). This is confusing. A keyword should mean a single thing. For me it is not a big deal to have to write self: Self or self: Self * over and over, and it would make the language simpler to just use first parameter for receiver. |
Beta Was this translation helpful? Give feedback.
-
Currently methods require annotating both the receiver name (
self
) and the receiver type (Self
orSelf*
), and require this to be done in a separate section delimited by square brackets (I'm usingself
instead ofme
in these examples as it's likely to be replaced: #1382):To reduce the amount of redundancy and boilerplate and to make it simpler and more familiar to users of existing languages, a couple of changes can be made:
self
name from the declaration since it's the same for every method. (Of course, inside the method, referring to the receiver would still happen withself
.self
could become a keyword to aid this.)Self
type to lowercaseself
keyword that means both the receiver type and the receiver value.addr
.self*
already means the receiver is passed by pointer.With these changes the above code would look like this:
Alternatively, if we want to be more familiar to C++ users, we could do like Swift: remove the explicit self parameter, use
static
(orclass
) keyword for class functions, andmutating
keyword for methods takingself
by pointer, and no additional keyword for non-mutating methods:These are just suggestions, there are other possible variations as well. Let me know your thoughts.
I might expand this post later to address the decisions in p0722 that lead to the current design, but what it mostly boils down to is: simple things should have simple syntax, reserve the more complicated syntax for advanced use cases. No one wants to write
[addr self: Self*]
over and over again.If, however, we only intend to provide methods for C++ compatibility, and want to discourage users from using them in pure-Carbon code, then it might make sense to require the current boilerplatey syntax.
Beta Was this translation helpful? Give feedback.
All reactions