2

I'm learning something from programming and I came to this:

While evaluating expressions, the result is automatically updated to larger data type of the operand. But if we store that result in any smaller data type it generates compile time error, due to which we need to type cast the result.

How to explain cast in the end of the paragraph? In this context, I would go as throw something away, but yet I don't get it

9

'type cast' is a single idea.
It's a coding/programming term meaning to specify a data type by forcing it, type-casting it - into the desired form.

For instance "3" is a 'string', which is a text data type. The quotes in programming make that quite specific; it's not a number any more, it's a string, even though the string contains what to a human looks exactly like a number. Computers don't see things the way humans do.

So - if we need to take that string & force it to become a number, we type-cast it to an integer [which is a whole number with no decimals]

Our string "3" then becomes the integer 3

in pseudo-code...

string myText = "3"; integer myNumber = (integer)myText;

That (integer) in brackets is the 'forcing', the type-cast.

Whether to hyphenate or not is probably a UK/US thing. I'm British, we hyphenate a lot more than the Americans.

I guess it could come from 'to throw' it into a particular type, but I'm not certain. To cast a spell would be a similar use - whether that's 'thrown' might be debatable.
Looking down the 10 meanings in the Oxford Dictionary for 'cast' I honestly wouldn't really know which it would fall under.
It might even be more akin to casting an object from molten metal.

| improve this answer | |
  • As an American, I use the compound word typecast. (And note that the "cast" you show usually isn't actually valid--that's a conversion. A cast in most languages would be Number → Integer.) – chrylis -cautiouslyoptimistic- Jul 29 '18 at 2:30
  • my example is actually syntactically correct in the only language I know how to write in, which isn't C or Lua or Haskell... it's LSL ;) – gone fishin' again. Jul 29 '18 at 8:14
  • 1
    My assumption has always been that the use of "cast" in this sense wasn't one of the ten meanings from cast(1) in your link, but rather cast(2): "Assign a part in a play or film to (an actor)", as though you're assigning this data the "part" of integer. Furthermore, "typecasting" is a thing that happens in acting as well: "Assign (an actor or actress) repeatedly to the same type of role, as a result of the appropriateness of their appearance or previous success in such roles." – Arclite Jul 29 '18 at 10:31
  • It's syntactically valid in most languages, just not semantically valid--LSL seems to be unusual in that it will perform a conversion. Most languages treat a cast as an assertion that an existing value is already actually of some other type. – chrylis -cautiouslyoptimistic- Jul 29 '18 at 17:16
7

Tetsujin explains the meaning in computer usage. I'd like to try to explain the etymology you're looking for.

In manufacturing, if a liquid is poured into a mold and allowed to harden into a solid that is determined by the mold, it is said to be "cast in that mold". Type casting is a method of taking a variable and changing its form to a new type or taking an object of unknown type and treating it as a specific type. Just like the liquid in the manufacturing process, it is "cast into the mold" of the type and takes on that form.

| improve this answer | |
  • Thank you. I'd later arrived at a similar conclusion, but wasn't certain. – gone fishin' again. Jul 28 '18 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.