2 ms·
> In "The C++ Programming Language", casting is described as a tool of last resort. Bjarne says he likes long winded cast operators because they stand out as an
by still_grokking 4y ago
> In "The C++ Programming Language", casting is described as a tool of last resort. Bjarne says he likes long winded cast operators because they stand out as an easily greppable marker of complex code.
The idea makes a lot of sense. Casting shouldn't be necessary at any time as this means you leave the safe space a static type-system gives you. At the point when you cast all bets are off; it's only on you to prevent type errors, the compiler can't help any more (ant this property of casts is frankly "viral", so it can affect code far away).
The problem with C++ is of course that they have a gazilion of ways to do (sometimes in surprising ways almost) exactly the same.
In Scala for example casts are also very explicit. You need to call the `.asInstanceOf[TargetType]` method. But that's the only syntax for this.
It makes really a big difference to have only one obvious way to do something. (Frankly Scala also doesn't always shine everywhere in this regard).
- peteradio 4y agoIts a bit like saying glue and nails isn't necessary to build a house. You should be able to do everything with precision joinery! Many times we are doing a renovation where a good portion makes it impossible to do precision joinery.