3 ms·
Slightly off topic: Why do we need to explicitly cast at all? If the language is strongly typed the compiler should know whether the types are compatible and d
by kwhitefoot 6y ago
Slightly off topic: Why do we need to explicitly cast at all?
If the language is strongly typed the compiler should know whether the types are compatible and do it for you or refuse to compile.
I suppose my point is that casting was a poorly chosen example because the obvious solution isn't to have a better notation but to have no notation at all and follow Python's lead with duck typing or functional languages and strong type inference.
- booleandilemma 6y agoWhat if you’re reading in a DateTime from a string in a file?
- throwaway894345 6y agoRequiring an explicit cast helps prevent type errors. It allows us to create types that mean something different than the original type. Consider a language with a “bytestring” type which is just any sequence of binary data and another “string” type which can only be valid utf8. The two types are compatible as far as the compiler is concerned. If the compiler could automatically cast between these types, we would find bugs from non-utf8 bytestrings passed into functions expecting utf8 strings.
- henrikeh 6y agoI think casting is an important part of documenting and encoding intent. Especially in embedded systems.
- andi999 6y agoOne example is if you want to use the / symbol for integer and float division. Also do you want every multiplication done in a type where the data fits in? (C promotes types a bit too agressively here). So I believe for a low level language there should be more casting and less automatic type promotion.
- imtringued 6y agoC++ has implicit casts and everyone regrets it. Imagine something asinine like assigning a tcp socket object to a string variable and not seeing a compile error.