Repository navigation
Write static properties that compile in C++ and are reachable from the class in Python [patch] - #209
Merged
Merged
Conversation
…e class in Python [patch] A static property's getter was lowered with IsReadOnly set, so C++ wrote `static int limit() const`, which g++ rejects. The getter is now read-only only when it is an instance member, and CppGenerator never writes `const` on a static member function. Python wrote a static property with bodies as an instance `@property`, so reading it from the class answered the property object. It is now written as `@staticmethod` getter and setter functions, `limit()` and `set_limit(value)`, as C, Rust and Go already lower it. Fixes #180 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EP9qqymiBp8RePCRRnDfQk
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #180
What changed
C++:
StandardLanguageGenerator.Separatedlowered every property getter withIsReadOnly = true, including static ones.CppGeneratorthen wrotestatic int limit() const, and g++ rejects that with "static member function cannot have cv-qualifier".IsReadOnly = !property.IsStatic.CppGeneratoralso writesconstonly when the function is not static. That covers a hand-built static, read-onlyFunctionDeclarationtoo.Separated(C, Go, Rust) give a static function no receiver, soIsReadOnlyon a static getter never reached their output. They are unchanged.Python:
GeneratePropertyDeclarationnever readIsStatic, so a static property with bodies became an instance@property. Read from the class, that answers the property object, not the value. It is now written as two@staticmethodfunctions,limit()andset_limit(value). This is the lowering C, Rust and Go already use, and both functions can be called on the class. An automatic static property is unchanged: it is still a class attribute.Tests
CppGeneratedSourceCompilesTests.AStaticPropertyGetterCompiles:Counter.Limit(static, getter returns 10) generatesstatic int limit()with noconst. A driver callingCounter::limit()compiles with-Wall -Wextra -pedantic -Werror.PythonGeneratedSourceImportsTests.StaticProperty_IsReachableFromTheClass: runs the generated module and checks thatCounter.limit() == 10, that it also works on an instance, and thatCounter.set_limit(3)can be called.With the generator changes reverted, both tests fail. With them, the full suite passes (1117/1117).
🤖 Generated with Claude Code
https://claude.ai/code/session_01EP9qqymiBp8RePCRRnDfQk
Generated by Claude Code