What is type erasure in C++?

Here’s a very simple example of type erasure in action: // Type erasure side of things class TypeErasedHolder { struct TypeKeeperBase { virtual ~TypeKeeperBase() {} }; template <class ErasedType> struct TypeKeeper : TypeKeeperBase { ErasedType storedObject; TypeKeeper(ErasedType&& object) : storedObject(std::move(object)) {} }; std::unique_ptr<TypeKeeperBase> held; public: template <class ErasedType> TypeErasedHolder(ErasedType objectToStore) : held(new TypeKeeper<ErasedType>(std::move(objectToStore))) {} }; … Read more

Java: getClass() of bounded type

If getClass() returns Class<? extends X>, nothing really bad can happen; actually it’ll help a lot of use cases. The only problem is, it is not theoretically correct. if an object is an ArrayList<String>, its class cannot be Class<ArrayList<String>> – there is no such class, there is only a Class<ArrayList>. This is actually not related … Read more

Type erasure, overriding and generics

The signature of fooMethod(Class<?>) is the same as the signature of fooMethod(Class) after erasure, since the erasure of Class<?> is simply Class (JLS 4.6). Hence, fooMethod(Class) is a subsignature of the fooMethod(Class<?>) but not the opposite (JLS 8.4.2). For overriding with instance methods you need the overriding method to be a subsignature of the overridden … Read more

Scala double definition (2 methods have the same type erasure)

I like Michael Krämer’s idea to use implicits, but I think it can be applied more directly: case class IntList(list: List[Int]) case class StringList(list: List[String]) implicit def il(list: List[Int]) = IntList(list) implicit def sl(list: List[String]) = StringList(list) def foo(i: IntList) { println(“Int: ” + i.list)} def foo(s: StringList) { println(“String: ” + s.list)} I think … Read more

Java erasure with generic overloading (not overriding)

Either rename the methods, or use polymorphism: use an interface, and then either put the clawback code in the objects themselves, or use double-dispatch (depending on your design paradigm and taste). With code in objects that would be: public interface Clawbackable{ void clawBack() } public class CommissionFacade { public <T extends Clawbackable> void clawBack(Collection<T> objects) … Read more

Type erasing type erasure, `any` questions?

This is a solution that uses C++14 and boost::any, as I don’t have a C++17 compiler. The syntax we end up with is: const auto print = make_any_method<void(std::ostream&)>([](auto&& p, std::ostream& t){ t << p << “\n”; }); super_any<decltype(print)> a = 7; (a->*print)(std::cout); which is almost optimal. With what I believe to be simple C++17 changes, … Read more

How to pattern match on generic type in Scala?

Maybe this will help def matchContainer[A: Manifest](c: Container[A]) = c match { case c: Container[String] if manifest <:< manifest[String] => println(c.value.toUpperCase) case c: Container[Double] if manifest <:< manifest[Double] => println(math.sqrt(c.value)) case c: Container[_] => println(“other”) } Edit: As Impredicative pointed out, Manifest is deprecated. Instead you could do the following: import reflect.runtime.universe._ def matchContainer[A: TypeTag](c: … Read more