-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SUGGESTION] Cleaner class declaration and implementation #120
Comments
I suspect that with cpp2 and modules, the idea of headers and class files is likely to be dropped altogether,
As such I suspect that if you want to present a public "header" you'd instead create a module that privately imported the classes module, just explicitly re-exported the public interface of said class, perhaps with some documentation.
While this feels like extra work, I'd conjecture that header + class files are already more complex than any other language and were required by the pre-processor and compiler, not by the core language itself, also I suspect that this will only be wanted when creating a publically distributed library
…________________________________
From: Morten Hattesen ***@***.***>
Sent: 15 November 2022 10:40
To: hsutter/cppfront ***@***.***>
Cc: Subscribed ***@***.***>
Subject: [hsutter/cppfront] [SUGGESTION] Class declaration and implementation: Reduce fragmentation and implementation leaking into headers (Issue #120)
Proposal to improve class declaration and implementation syntax
class implementation details leak into declaration
Any private member variables and methods of a class implementation have to be expolicitly declared in the class declaration (header), leading to implementation details leaking into the (public) class interface. This causes a cognitive overload of irrelevant clutter to class users, causes dependent code to require recompilation, and causes the (public) interface to mutate more frequently then necessary when changes to the implementation are made.
Proposal: a new syntax allows the class declaration (header) to remain free of implementation details (private member variables and methods) leaking into the public/published interface.
Implementation: This proposal could be implemented by synthetically allocating and generating a PImpl<https://en.cppreference.com/w/cpp/language/pimpl> (pointer to implementation) in every class declaration, and causing all private member variables and methods defined in the class implementation (cpp), but not declared in the class declaration (.hpp), to become member of this automatically generated PImpl struct/class. Although this proposal will lead to every class incurring the overhead/cost of an additional pointer, this could be made optional, controlled by a (pre-)compiler option and/or a class declaration keyword to indicate the desired (implicit) use of the PImpl idiom.
C++ class implementation source is fragmented
The current syntax for implementating class methods leads to fragmented code with a large amount of repetitive "noise" with each method having to declare the class for which the method is implemented void my_class::my_method(). The encapsulation that OOP provides is not carried over into the source code.
Proposal: class implementation syntax encapsulates the implementation source within a class implementation block, and removes the need for each method to explicitly declare which class it belongs to, e.g.
//my_class.cpp
class my_class impl {
void my_method() {
//...
}
}
Adopting above suggestions will lead to
* Cleaner (more readable) class declaration syntax that does not contain leaked implementation details or explicit Pimpl forward declaration
* Better separation between class/interface declaration and implementation
* More stable class declaration causing less need for recompilation when implementation changes are made
* Cleaner (more readable) class implementation syntax
* Better encapsulation (less fragmentation) of class implementation source
—
Reply to this email directly, view it on GitHub<#120>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQKZ5LDXYGPDKFOLYF3WINSATANCNFSM6AAAAAASAZGVLU>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Need to confront that with modules. |
Thanks for the suggestions! I hope to implement classes over the winter vacation (if I'm lucky!) and I hope you'll find the design addresses the concerns above. Also, as some have noted, Cpp2 is a modules-first design, and Cpp2 won't have header files or separate declarations from definitions, which simplifies some of these concerns. More in the coming months... again, thanks. |
Proposal to improve class declaration and implementation syntax
class implementation details leak into declaration
Any private member variables and methods of a class implementation have to be expolicitly declared in the class declaration (header), leading to implementation details leaking into the (public) class interface. This causes a cognitive overload of irrelevant clutter to class users, causes dependent code to require recompilation, and causes the (public) interface to mutate more frequently then necessary when changes to the implementation are made.
Proposal: a new syntax allows the class declaration (header) to remain free of implementation details (private member variables and methods) leaking into the public/published interface.
Implementation: This proposal could be implemented by synthetically allocating and generating a PImpl (pointer to implementation) in every class declaration, and causing all private member variables and methods defined in the class implementation (cpp), but not declared in the class declaration (.hpp), to become member of this automatically generated PImpl struct/class. Although this proposal will lead to every class incurring the overhead/cost of an additional pointer, this could be made optional, controlled by a (pre-)compiler option and/or a class declaration keyword to indicate the desired (implicit) use of the PImpl idiom.
C++ class implementation source is fragmented
The current syntax for implementating class methods leads to fragmented code with a large amount of repetitive "noise" with each method having to declare the class for which the method is implemented
void my_class::my_method()
. The encapsulation that OOP provides is not carried over into the source code.Proposal: class implementation syntax encapsulates the implementation source within a class implementation block, and removes the need for each method to explicitly declare which class it belongs to, e.g.
Adopting above suggestions will lead to...
Example of proposed CPP2 syntax
The text was updated successfully, but these errors were encountered: