hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.52k stars 246 forks source link

[SUGGESTION] Cleaner class declaration and implementation #120

Closed hattesen closed 1 year ago

hattesen commented 1 year ago

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.

//my_class.cpp`
class my_class impl {
  void my_method() {
    //...
  }
}

Adopting above suggestions will lead to...

Example of proposed CPP2 syntax

//Declaration: my_class.hpp`
class my_class {
public:
  void my_method();
private:
  int my_var;
  // CPP2: Automatic declaration of my_class::PImpl (forward class and pointer)
  // CPP2: no need to declare private implementation member variables and functions in header
}
//Implementation: my_class.cpp`
class my_class impl { // impl keyword provides context for member variables and functions
  void my_method() { // my_class:: prefix not required
    // implementation
  }
private:
  // private (undeclared) member variable used by implementation
  int var_impl {0}; // CPP2: implicitly declared in my_class::PImpl
  // private (undeclared) member function used by implementation
  void method_impl() { //CPP2: implicitly declared in my_class::PImpl
    // implementation
  }
}
SebastianTroy commented 1 year ago

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 PImplhttps://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

— Reply to this email directly, view it on GitHubhttps://github.com/hsutter/cppfront/issues/120, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AALUZQKZ5LDXYGPDKFOLYF3WINSATANCNFSM6AAAAAASAZGVLU. You are receiving this because you are subscribed to this thread.Message ID: @.***>

filipsajdak commented 1 year ago

Need to confront that with modules.

hsutter commented 1 year ago

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.