Most active commenters
  • Maxatar(3)
  • kccqzy(3)
  • account42(3)

←back to thread

196 points svlasov | 25 comments | | HN request time: 0.422s | source | bottom
1. Dwedit ◴[] No.40851285[source]
Compile time or runtime? Compile time reflection would be completely painless and bloat-free.
replies(3): >>40851300 #>>40851330 #>>40851736 #
2. armchairhacker ◴[] No.40851300[source]
Compile-time (“static reflection”)
3. shortrounddev2 ◴[] No.40851330[source]
RTTI is a super vital feature for deserializing without having to generate code or write a ton of tedious boilerplate. I'd be very happy with RTTI in C++ and my life would be instantly easier if there were RTTI in typescript so I didn't have to use any of the hacky solutions out there for deserializing JSON on backends without RTTI.

I suppose C++'s template system might be able to generate JSON deserializers with static reflection as well

replies(2): >>40851369 #>>40851644 #
4. chongli ◴[] No.40851369[source]
You don't need RTTI to deserialize data in a clean way. What you need is return-type polymorphism. Haskell has this and it makes writing serializers and deserializers symmetric and totally painless.
replies(1): >>40851442 #
5. fooker ◴[] No.40851442{3}[source]
Return type polymorphism and inheritance doesn't mix very well.

Swift got into this mess early in it's lifecycle and it's type checking is still more expensive than the rest of the compiler combined, and unpredictable on top of that.

replies(2): >>40851982 #>>40852119 #
6. comex ◴[] No.40851644[source]
> I suppose C++'s template system might be able to generate JSON deserializers with static reflection as well

It definitely can, and it will be faster and more type-safe than doing it at runtime. But if you do want to do it at runtime, well, it's possible to implement runtime reflection as a library on top of compile-time reflection, so someone will probably do that.

7. thechao ◴[] No.40851736[source]
Having implemented reflection in languages like C(++), before, it is most certainly not bloat-free. There are sorts of 'obvious' things programmers do (like enum-reflection) that end up injecting strings all over the place. The overhead is (worst case) proportional to the source-code size, in those cases. In other cases, you end up with bloat proportional to heavily-utilized template libraries. However, unless the reflection system is very clever, i.e., exposes enough information to the linker to strip duplicate-ish symbols, you end up with a bunch of reflection copies.
replies(2): >>40852644 #>>40852761 #
8. chongli ◴[] No.40851982{4}[source]
Yeah if you ask me, inheritance is the one to go. Every time. Inheritance just makes things more complicated. It’s not a great tool of abstraction.
9. Maxatar ◴[] No.40852119{4}[source]
C++ has both return type polymorphism and inheritance. What it doesn't have is the kind of type inference that Haskell has. Its type inference is very limited.
replies(1): >>40853546 #
10. rerdavies ◴[] No.40852644[source]
extern templates address the issue of having multiple instances of a template being expanded inline, with only minimal mess and fuss. (A way to prevent inlining of templated code would have been nice too).
11. zarzavat ◴[] No.40852761[source]
I always thought it’s good practice in C/C++ to have only one translation unit in release builds e.g. SQLite amalgamations, instead of relying on LTO. It also speeds up compilation because it isn’t recompiling the same header files over and over again.
replies(2): >>40853586 #>>40854808 #
12. fooker ◴[] No.40853546{5}[source]
You can't have two functions that differ in just the return type in C++
replies(3): >>40853661 #>>40853704 #>>40857429 #
13. kccqzy ◴[] No.40853586{3}[source]
It's a cute idea but just not scalable. It doesn't speed up compilation because separate translation units can be compiled independently and cached.
replies(2): >>40855459 #>>40855583 #
14. slaymaker1907 ◴[] No.40853661{6}[source]
You can sort of do it so long as the return type is a template parameter.

    template<typename T>
    T my_construct() { T result; return result; }
replies(1): >>40855598 #
15. pritambaral ◴[] No.40853704{6}[source]
You can, if you abuse C++ enough. I needed this very thing for a custom DSL.

    struct PolyReturn {
      const int value;
      operator int() const { return value; }
      operator bool() const { return value > 0; }
    };

https://cppinsights.io/s/f0b9976f
16. leni536 ◴[] No.40854808{3}[source]
That's called a "unity build", build systems such as cmake support it. It has its pros and cons.
replies(1): >>40855558 #
17. zarzavat ◴[] No.40855459{4}[source]
Do things that don’t scale :)

Obviously it’s not suitable for dev builds, I don’t think anyone uses it for that. For release builds you would want to clear caches anyway.

replies(1): >>40859831 #
18. account42 ◴[] No.40855558{4}[source]
And while it helps with compile time (especially with mediocre compilers like MSVC where a large time is spent in the front-end), it doesn't help with stripping unneeded code/data compared to LTO.
19. account42 ◴[] No.40855583{4}[source]
It does significantly speed up clean builds in many cases, to the point that those clean builds can even be faster than many "incremental" builds in some cases.
replies(1): >>40859824 #
20. account42 ◴[] No.40855598{7}[source]
That's not polymorphism as that template parameter won't be deduced in any context and you will always have to explicitly instantiate the template.
replies(2): >>40856673 #>>40857868 #
21. Maxatar ◴[] No.40856673{8}[source]
That is still polymorphic, but as I mentioned C++ does not do the kind of type deduction that Haskell supports so you do have to explicitly instantiate the template. However, you can instantiate it based on context, for example using decltype.
22. Maxatar ◴[] No.40857429{6}[source]
You certainly can and I know there are several JSON libraries that do it. The gist of it is to write a struct and overload the conversion operator for the set of return types you want to support, for example overload the conversion operator to int and std::string. As soon as the instance of that struct is used in a context that needs an std::string, then it calls the string overload.
23. gpderetta ◴[] No.40857868{8}[source]
challenge accepted: https://gcc.godbolt.org/z/bGdP79aEj

edit: you get pseudo call-by-name as a bonus.

24. kccqzy ◴[] No.40859824{5}[source]
I do not know of any compiler that can compile different parts of a translation unit in parallel. But plenty of systems (including the venerable `make -j`) can compile separate translation units in parallel because they are independent.
25. kccqzy ◴[] No.40859831{5}[source]
You don't want to clear caches for release builds. That just makes release builds unnecessarily slow and impedes the flow of Continuous Deployment. You just need to have separate caches for different build flags, including a dedicated cache for release builds.