←back to thread

196 points svlasov | 1 comments | | HN request time: 0s | source
Show context
Dwedit ◴[] No.40851285[source]
Compile time or runtime? Compile time reflection would be completely painless and bloat-free.
replies(3): >>40851300 #>>40851330 #>>40851736 #
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 #
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 #
fooker ◴[] No.40851442[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 #
Maxatar ◴[] No.40852119[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 #
fooker ◴[] No.40853546[source]
You can't have two functions that differ in just the return type in C++
replies(3): >>40853661 #>>40853704 #>>40857429 #
slaymaker1907 ◴[] No.40853661[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 #
account42 ◴[] No.40855598[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 #
1. Maxatar ◴[] No.40856673[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.