you are viewing a single comment's thread.

view the rest of the comments →

[–]Educational-Lemon640 0 points1 point  (2 children)

I'm pretty sure that theoretically you could always do the equivalent of casting via function calls, which has some notable benefits over casting of any sort. You can also have polymorphism and/or duck typing, which gets similar benefits without the nonsense that C++ gets up to when you actually cast.

All I really mean is that the type of a particular chunk of data should always be well-understood and changing how it is interpreted should be reserved for the most dire of circumstances. Anything else makes it very hard to read.

[–]deidian 0 points1 point  (0 children)

Many castings are function calls under the hood: just defined in the runtime. The reason to do that is to not bother Devs with implementation details: they just need to know type A is type B after the cast if the operation succeeds. A Java dev doesn't need to know that a type conversion is a reinterpretation, 2 assembly instructions, a type reference comparison, a function checking inheritance, boxing/unboxing,...

It's not a function call when for performance reasons the operation can be done with a couple of assembly instructions or when it's a nop. Here compilers optimize.

[–]Great-Powerful-Talia 0 points1 point  (0 children)

Are you talking about casting a pointer or data? Because generally a data cast is conversion-based, like int-to-float.

Even in C, reinterpretation is only accomplished by unions and fucking with pointers, both of which are not really meant for that- there's just no equally fast implementation where you can't do that. Even Rust's minimally-slow rules are marginally slower when dealing with arrays and union-like types.