-
Notifications
You must be signed in to change notification settings - Fork 269
rust-like impl / trait ergonomics? #1427
-
I'm not particularly a fan or even a user of Rust, but separating data, interface, and members via impl appears quite pleasing. I'd suspect that similar functionality could be achieved with metaclasses and reflection, but cpp2 also provides an opportunity for exploring this in a more macro-like sense: simply merging the pieces together as needed at transpilation time.
I'm making this discussion less of a suggestion and more just because I'm interested to hear any opinions or prior-art on this.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments
-
Two points that would worry me from a maintenance/onboarding perspective:
- The struct definition does not let you know that there is an
implblock. So you have to be aware that these exist. (There can be multiple ones.) So: How do you know, just a looking at thestructdefinition, which methods are defined forRectangle? This would make it quite hard for new people to understand what is possible. - The same is basically true for traits. You do knot know for which types it is implemented.
It seems like a nice idea. In larger projects the use of this feature has to be probably very restricted in order to make it maintainable.
Just my 2 cents.
Beta Was this translation helpful? Give feedback.
All reactions
-
Is the point that there may be cases where it is better that you don't have to be aware of every impl block? If there is a specific project where it is convenient to add a is_all_whitespace() method to the rust String class you can effectively add that method using impl. That doesn't mean that every rust programmer should use or know about this impl block.
Maybe adding impl / trait ergonomics has less value for cppfront given that cppfront has unified function call syntax where you can call f(x) as x.f(). In this example you could write a function is_all_whitespace(line : std::string) and then using UFCS you could invoke that function in a way that looks like it is a member function: my_line.is_all_whitespace().
Beta Was this translation helpful? Give feedback.
All reactions
-
Agreeing with what @ktegan said, universal function call syntax and C++'s duck typing already solve the problems that impl would be solving. C++ concepts define a required interface for supplied types, and if the type you're passing doesn't fit that interface, you can write a free function that will be used by that interface with UFCS.
Beta Was this translation helpful? Give feedback.