8 ms·
Metaclasses: Thoughts on generative C++
- lultimouomo 9y agoA snippet from the paper that made me understand what this is about: // $ is the reflection operator and T is a type. constexpr { // execute this at compile time for... (auto m : $T.variables()) // examine each member variable m in T if (m.name() == “xyzzy”) // if there is one with name “xyzzy” -> { int plugh; } // then inject also an int named “plugh” } This would inject "int plugh;" in the scope surrounding the constexpr block.
- ta834939874 9y agoreminds me of http://terralang.org/ http://terralang.org/
- tomovo 9y agoalso Jai: https://www.youtube.com/watch?v=gWv_vUgbmug https://www.youtube.com/watch?v=gWv_vUgbmug
- _pmf_ 9y agoI have hopes that Jai will be at a very nice local optimum regarding performance and productivity, but I think just like in C++, safety is not a major design goal (you need to be unsafe if working with memory mapped structs).
- Rusky 9y ago> you need to be unsafe if working with memory mapped structs Perhaps when defining their address and layout, or if they are a hardware structure that affects memory like a page table or TLB or a DMA controller, but beyond that, why? Or is that just the main use case you're describing?
- JoshTriplett 9y agoThis kind of compile-time code is a great substitute for things people currently do with template metaprogramming or code generators.
- pzone 9y agoWow, not sure what to think of this. Seems like this merely grows the monster that is the C++ specification, yet at the same time I wouldn't mind wiping out all that boilerplate.
- pjmlp 9y agoCan you fit the Python 2.x and 3.x differences all on your head, including differences between each single release and all the included batteries?
- makapuf 9y agoI'm not sure you can compare the complexity of using the majority of Python3 to modern C++. Besides, the switch to python 3 happened once in ten years. The complexity of the change is I think lower than the introduction of c++11 or 14. BTW I like C++ but its complexity is daunting. What subpart is relatively simple to use ? How would you start now ?
- dom0 9y agoC++ at least has a readable specification.
- skolos 9y agoI like the flexibility that this feature adds. However I find that C++ is hard to use without good IDE support and it looks like this feature will not be properly supported in IDE's for very long time.
- maccard 9y agoThis feature isn't even supported in compilers yet.
- Animats 9y agoAre they trying to catch up to Rust while maintaining backwards compatibility? Or what? C++ needs the full definition of a class, including its private members, before you can use its methods. This is a huge dependency headache. Most later languages have interfaces or traits or something to decouple class definition and interface definition. This looks like an attempt to deal with that. Of course, this is a retrofit, so it's going to be messy. C++ has a lot of baggage. It may have exceeded its excess baggage allowance. The basic problem with C++ is that it has heavy encapsulation without memory safety. No other language has that. Either nothing is hidden, as with C, or the language is memory-safe, as is almost everything since C++.
- justicezyx 9y ago> full definition of a class Pointers and virtual class, why do you need the definition to use the interface? Could you provide more details? > The basic problem with C++ is that it has heavy encapsulation without memory safety. It's pretty simple to write memory safe code in c++, and encapsulation is a feature that not mandated in c++ code.
- adrianN 9y agoPointers and virtual classes have runtime overhead. It's not simple to write memory safe code in C++, otherwise we would have a lot fewer vulnerabilities in C++ codebases.
- Negitivefrags 9y agoYou could make one hell of a good C++ web framework with this. http::controller mypage { //Browsing to http://server/mypage/test?some_str=blah¶m=5 calls this http::response test( const std::string& some_str, const unsigned param ) { return web::response{ 200, "You did awesome stuff" }; } //This method can only be called with HTTP POST [[POST]] http::response save_some_data( const std::string& blah ) { return 200; } }; int main() { http::server server( "localhost", 80 ); server.add_route( "mypage", std::make_unique< mypage >() ); server.run(); } It's all the magic of a modern web framework but in a fast language. Due to web assembly you can easily have your server and client side scripting be done in the same language, with the same libraries which is also huge bonus. I honestly can't think of a better stack.
- wocram 9y agohttps://rocket.rs/ https://rocket.rs/ is basically this, but in rust instead.
- skocznymroczny 9y agohttps://vibed.org/ https://vibed.org/ is basically this, but in D instead
- Sharlin 9y agoWith the function-style syntax and general compile-time programming support this is getting awfully close to full hygienic macro capacity, even if restricted to outputting type declarations at first.
- nialv7 9y agoGlad to see C++ become more and more D
- mlvljr 9y agoTerrified.
- eigenbom 9y agoI'm excited by this feature, along with all the other compile-time programming stuff (reflection, concepts). The downside I see is having to remember how a metatype works.. e.g., the default access type for member variables of struct, class, interface, value, .. and on and on. But if we keep the number of metatypes low, this may manageable.
- manyoso 9y agoHe's basically suggesting writing plugins for the compiler.
- mannykannot 9y agoAnything Mr. Sutter says is well worth consideration, and this is no exception. I would like to raise one concern, however, that applies to all such proposals for simplifying programming by adding more powerful abstractions to the language: I hope as much effort is put into debugging, at the semantic level of the source code, as is put into generating code. In this regard, this quote from the paper leaves me feeling uneasy: 'Enable writing many new “specialized types” features (e.g., as we did in C++11 with enum class) as ordinary library code instead of pseudo-English standardese, with equal usability and efficiency, so that they can be unit-tested and debugged using normal tools' (my emphasis.) Sometimes, of course, you have to go below that level to figure out what is wrong, but is better if you do not have to do so unless it is unavoidable in principle.
- iainmerrick 9y agoMetaclasses for things like "interface" and "value" sound fantastic, and will get rid of huge amounts of boilerplate, but I wonder how flexible they really are. I don't immediately see a way to implement something like C#'s extension methods, or Objective-C categories, for example -- extending classes with new methods. Those are incredibly useful when integrating with an existing codebase. Metaclasses just seem like a way to do existing C++ stuff in a cleaner and terser way. Definitely very useful but maybe not a huge game changer. I'm also concerned at the potential impact on compile times once people start using metaclasses very heavily; but maybe it won't be as bad as templates in that respect.
- tmpmov 9y agoJust as others have noted, I'm worried about the intermediate representation hiding necessary details. Here's an example: If I'm looking at one of these classes that have been compile time injected with new functionality, is there an easy way to see what's been added without looking at these compile time injections? Specifically, it would be great if there was support for looking at both the source code before the injections and the source code after the injections in an easy way (built in IDE support for example). While different from what's being proposed here, C/C++ macros can be used for massive amounts of code generation, but looking behind the curtain of the macros involves running your source code through the preprocessor. Getting to the intermediate representation for macros feels tedious and most IDEs have limited support to aide the programmer pulling the veil off of macro code generation. I can imagine (*though maybe wrong) that a similar problem exists for certain types of template meta programming.
- tmpmov 9y agoNote that I think this is still a great idea. In fact, I'm rooting for it to come to C++! However, I think IDE plugins will need to catch up (shouldn't be too bad if metaclasses are adopted and once clang et al add support). Very neat stuff.
- dmytrish 9y agoThe thing that makes me uneasy about the proposed implementation of metaclasses is that imperative code is used for code generation instead of declarative specifications. Simple examples are relatively simple and predictable, but anything more complex might blow up into incomprehensible mess pretty quickly.
- Too 9y agoDid I miss something or is this restricted to "inheritance" instead of composition? As example I can't make a constraint/metaclass that is both plain_struct, ordered and final at the same time, maybe that particular example doesn't make sense practically but I'm sure there are others that do.