Simpler, more flexible dereferencing syntax #1932
Replies: 1 comment 7 replies
-
I expect this to come up for other operators besides For I think some folks (notably @chandlerc) have also argued that prefix |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Issue
In C++, dereferencing is simple in the beginning, but can become awkward pretty quickly:
val.member
vsptr->member
(***ptr).member
vs(**ptr)->member
(*ptr)->method((*ptr2)->member)
(e.g. pointers to wrappers with an overloaded->
)(*(*(*ptr)->m1)->m2)
(admittedly, this last example possibly goes against good OOP practice, but makes for a good showcase imo)Proposal
Make the unary
*
a postfix operator instead, and in the process->
also becomes redundant. The above would become:val.member
vsptr*.member
(no need for->
anymore)ptr***.member
ptr**.method(ptr2**.member)
ptr**.m1**.m2*
Imo, this makes both the language itself and user code simpler, makes counting the number of derefs easy, and frees up the
->
for future uses.Possible disadvantages
->
. In Carbon's case, imo it'd make the most sense to rethink operator overloading from the ground up, rather than try to match C++ feature-by-feature.*
to be inherently more readable to a broader range of people, but I don't know & can't comment as to the reasoning behind it. Unary operators look a lot better as postfix to me personally, since they allow a more consistently pipeline-like LTR-readable syntax.Beta Was this translation helpful? Give feedback.
All reactions