carbon-lang
carbon-lang copied to clipboard
out-of-line destructor syntax seems inconsistent with other declaration forms
Our syntax for an out-of-line definition of a destructor looks like this:
class C {
destructor [self: Self];
}
destructor C [self: Self] {
}
In all other declaration forms, when we have introducer_keyword identifier, the identifier is the name of the entity being declared. However, in this case, the name C isn't being declared; rather, its destructor is being declared. This seems inconsistent.
#1154 doesn't list any considered syntactic alternatives for its chosen form of destructor (as a special function-like member). Here are a few we should explicitly consider:
- Using regular function syntax with a special name, instead of a special introducer:
fn <special>[self: Self]();andfn Class.<special>[self: Self]() {}. Choices for the "special name" include:destructor, where we keepdestructoras a keyword:fn destructor[self: Self]();orfn Class.destructor[self: Self]() {}. This somewhat parallels what we do with thebasekeyword as a pseudo-member-name.~:fn ~[self: Self]()orfn Class.~[self: Self]() {}. This mirrors the current idea of using~as an operator to express destructive ownership transfer.~Self:fn ~Self[self: Self]()orfn Class.~Self[self: Self]() {}. This would be very C++-like.
- As previous option, but without the
(). - In the out-of-line case, add the
forkeyword afterdestructor:destructor for C [self: Self] {}.
Hi! i wanna work on this. Please assign me this task,
@AnonymousDeveloper156 See our contributing guide:
When triaging issues, we typically won't assign issues because we want to be confident that contributors who have an issue assigned to them are planning for the amount of time it will take, which requires familiarity. Contributors with write access are expected to have that familiarity and may assign issues to themselves.
Hii , please assign this to me I know about it
I started a discussion in Discord about potential names for the fn <special>[self: Self]() case. We came up with a couple of options to consider:
deletedestroydestruct
One potential advantage for all three is that they are verbs, which matches the common naming of a function like this.