Allow readonly
/ const
keyword for class member function definitions
#47003
Labels
Duplicate
An existing issue was already created
Suggestion
π Search Terms
readonly class member function assign overwrite
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Class definitions should provide a way to protect member function definitions from accidentally being overwritten.
The syntax
public readonly callback() { ... }
is currently an error, as ispublic const callback() { ...}
. I can see where some people might get confused or surprised about the use of one of these keywords, thinking it applies to the return value, so I'm open to other suggestions -- but frankly, the current behavior is surprising.π Motivating Example
This is currently valid code:
As far as I can tell, the only way to catch the above logic error is to declare all methods of
Foo
asconst
member variables, e.g.π» Use Cases
This probably doesn't actually come up that often in the wild, but it can produce difficult-to-trace logic errors at runtime when it does. The workaround posted in the previous section is unwieldy, and some syntactic sugar would make the feature more likely to be used.
The text was updated successfully, but these errors were encountered: