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

I tried, but I never could understand cast in C or anywhere else. On the other hand, for decades I've seen, seen documentation for, and used several data type conversions among common elementary data types -- binary, 2's complement integers, decimal, base 2 floating point, base 16 floating point, precision differences, character strings, etc.

So, on the one hand there is cast, and on the other hand there is data type conversions. Cast is usually documented as just a cast where the meaning of that word is not in any dictionary and is not well explained otherwise. So, with a cast, often are getting a data conversion of some kind, but are usually missing the documentation to know just what.

My best explanation: C wanted some cases of strong typing, e.g., so that pointers could have data types and, thus, the relatively heavy use of pointers would cause less trouble. But a cast was an instance of overriding the usual strong typing. So, really, cast was presented as a way to override strong typing, but the issue of just how the data conversions would be done was left poorly or undocumented.

When I do a data conversion, I'm fully aware of the violation of strong typing -- I know that and am not concerned. But what I am concerned about is just how the data conversions are done, and in C, etc., I don't see the documentation to tell me. In strong contrast, PL/I was very careful to document how the data conversions would be done.

So, each time I see cast without details of how the data conversions are done, I'm ready to scream.

Am I the only one who cares about the details of the data conversions than the violation of strong typing? Again, to me, the main issue is the data conversion, not the strong typing.




Cast (according to the C99 standard, section 6.3, linked at bottom): A construction formed by preceding an expression by a parenthesized type name, converting the value of the expression to the named type.

So in C, casting is a language syntax that provides and explicit way to specify type conversion (rather than the implicit conversion that happens in cases like promotion). Most type conversions

Section 6.2.5 contains the defined type conversions, but most of what I'd consider edge cases are "implementation-defined" or have "undefined behavior".

Does any of this bother me? No, not really. No more than it bothers me that there isn't a single, authoritative, and clear definition of what constitutes "strong typing" and which operations are a "violation" of it.

(Linked from Wikipedia as being the C99 standard "effectively available for free"): http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf


Thanks for the reference.

Gee, that's a long way ahead of what I got out of reading K&R!

In

> So in C, casting is a language syntax

right. So, cast is just syntax but short on semantics, that is, how the conversion is actually done so that we can know the results, that is, if you will, what the heck happens.

In

6.3.1.5 Real floating types

is

> If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined.

So, yes, this is an example of where I'd be concerned.

On "strong typing", I always thought that the concept was poorly defined and described but never screamed bloody murder about it if only because it didn't directly affect what my software would do. I used "strong typing" in my post if only because it seems to be what the C community likes to do, and when in Rome do as Romans do. Or, maybe strong typing is better than weak typing or typeless -- okay by me.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

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

Search: