5 ms·
Supporting typecasting is hardly earth-shattering. Pretty much every statically typed programming language in actual use supports typecasts. That includes Sca
by cmccabe 13y ago
Supporting typecasting is hardly earth-shattering. Pretty much every statically typed programming language in actual use supports typecasts. That includes Scala, Java, C++, C, C#, Pascal, Algol, and even Ada.
This title should be "stunned by shitty poorly documented library function" not "stunned by Go."
There are a few languages out there that don't support typecasting. I think SML was one, for example. But they made a lot of other design choices to be able to support that choice, and so far that set of choices has not gained popularity. At minimum, you would need a fairly complex type system with generics to be able to completely forbid typecasting. You also run into problems where stable APIs completely block progress in a particular area.
- pcwalton 13y ago> 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.)
- nly 13y agodynamic_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.
- marshray 13y agoI 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.
- nly 13y agoWell protected is the default.
- pjmlp 13y agoThe default is private for classes and public for structs. Section 11.2 of the C++ standard.
- pjmlp 13y agoBy 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.
- cmccabe 13y agoIt'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.
- tel 13y agoAlso, 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.
- alexchamberlain 13y agodynamic_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.
- dbaupp 13y ago> Supporting typecasting is hardly earth-shattering I think the problem is the standard library using it extensively, apparently without doing it properly. As an example, C++ has casting but the STL doesn't need to use it (much) because it has generics in the form of templates.
- tel 13y agoI happily use a coercion-free language every day: Haskell. I don't miss coercion at all. Being able to trust the type contracts that each function portrays is an incredibly important part of the confidence that Haskell gives a developer, team, or leader.