Hacker News new | past | comments | ask | show | jobs | submit login

> There are a few languages out there that don't support typecasting. I think SML was one, for example.

Haskell too, unless you explicitly opt in with Data.Typeable.

It's arguably true in a certainly commonly-used subset of C++ as well: this type of typecast doesn't work in C++ unless you use dynamic_cast or another RTTI system (like COM). dynamic_cast only works on classes with a vtable. Many C++ projects don't use any form of RTTI. (Note that the Go code does not assert that the Reader is a ReadClose; rather it depends on RTTI to determine whether the reader is a ReadClose.)




Also, used normally, the best you're going to get out of Data.Typeable is cast :: a -> Maybe b which means that if your two types don't have runtime equivalence you'll be handled a statically ensured runtime Nothing that you must handle gracefully. The semantics are extremely clean.


dynamic_cast only works on classes with a vtable This isn't technically true - theoretically, you could implement C++ without vtables whatsoever. Furthermore, you could use a static_cast if the pointer had originally been cast from something lower down the hierarchy.


dynamic_cast downcasts in C++ only work across public inheritance. In particular a object that inheritance a Closable interface privately and a Reader interface publicly wouldn't allow a cross cast.

dynamic_cast isn't a loophole in the type system, it's there to strengthen it.


I don't think I've ever seen C++ that inherited an interface privately.

That said, it's extremely common for classes to lack any virtual functions and thus be unusable with dynamic_cast.


By default it is private.

Usually private inheritance is mostly used for library code, as a way for code reuse but without exposing the inheritance relationship to the class users. Also coupled with mixins sometimes.


Well protected is the default.


The default is private for classes and public for structs.

Section 11.2 of the C++ standard.


It's arguably true in a certainly commonly-used subset of C++ as well: this type of typecast doesn't work in C++ unless you use dynamic_cast or another RTTI system (like COM). dynamic_cast only works on classes with a vtable.

It's true that RTTI isn't used very much in C++, but I can't think of any non-trivial C++ project that doesn't include some traditional unsafe typecasts. And I have worked on a lot of C++ projects. And then there's the implicit type coercions, and the implicit constructors... ah, the good old days.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: