[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::languages

Title:Languages
Notice:Speaking In Tongues
Moderator:TLE::TOKLAS::FELDMAN
Created:Sat Jan 25 1986
Last Modified:Wed May 21 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:394
Total number of notes:2683

347.0. "difference between casting and coercion?" by STAR::ABBASI (iam your friendly psychic hotline) Fri Dec 18 1992 15:50

    what is exactly the difference between casting and coercion
    in compiler related context?

    i sort of forgot. is it that casting is explicit request by the
    user to change the data type of the object, and coercion is
    dont by the compiler without the user asking for it?

    /nasser
    who_does_not_have_a_compiler_book_around_to_look_it_up

T.RTitleUserPersonal
Name
DateLines
347.1Bit-preserving vs semantics-preservingTLE::JBISHOPFri Dec 18 1992 16:1417
    It depends on who you ask (I believe C has different meanings),
    but from the VAX Pascal perspective,
    
    o	casting is an explicit request to re-interpret a set of
    	bits (which are the representation of a particular value
    	of a particular data-type) as being a representation from
    	a different data-type, e.g.  You put 3.14159 into a float
    	and then view it as a character string (and get nonsense);
    
    o	coercion is the implicit conversion of one data-type to 
    	another in such a manner that the "meaning" is maintained,
    	e.g. you put 3 into an integer and then multiply it by
    	2.1 (a float), so the the 3 is coerced into 3.0.  Note
    	that the bit pattern representing 3.0 can be quite different
    	from that which represents 3.
    
    		-John Bishop
347.2Casting a gauntletTLE::AMARTINAlan H. MartinMon Dec 21 1992 22:3412
Re .0:

I agree with your definitions, for both C, and in general.


Re .1:

I daresay the definition of "casting" you attribute to the VAX Pascal
perspective is commonly known as "representational viewing".  Also, no decent
context comes to my mind where it has that definition you give.  (Ask me what
indecent context in which it arose).  Got any good references?
				/AHM/THX
347.3Pascal doc set good enough?TLE::JBISHOPTue Dec 22 1992 10:309
    _Programming_in_VAX_PASCAL_, section 7.2.6, p 7-12:
    
    "...the actual representation of the object being cast is never
    altered by the type cast operator."
    
    There's an example of casting a float as a record to look at the
    mantissa, etc.
    
    			-John Bishop
347.4VAX Pascal ftagn?TLE::AMARTINAlan H. MartinTue Dec 22 1992 12:136
Re .3:

Thanks, that's as horrifically authoritative as one could want.  Pray tell, is
this feature (or nomenclature) part of any ISO Pascal standard, or adopted
from another vendor's Pascal, or is it a Digital extension?
				/AHM/THX
347.5What level magician are you?STEVEN::HOBBSMon Dec 28 1992 10:3116
The VAX Pascal concept of a "cast" came from an early definition of
Ada.  There are "casts" and there are "conversions".  Conversions are
what programmers usually do; casts are what magicians usually do.  In
a conversion, the interpretation of the value never changes although
the representation may change.  In a cast, the representation never
changes although the interpretation of the value may change.  (Note
that sometimes programmers are also magicians when they need to cast a
spell over a particularly recalcitrant piece of data).

The VAX Pascal use of "cast" is in conflict with the Algol-68 use of
the term.  In Algol-68, a cast always had the semantics of a
conversion of some sort.  In C, the term cast is not used uniformly.
In some cases, a cast in C will change the representation without
changing the value while in other cases a cast in C will change the
value without changing the representation.  (On the other hand, C is a
language for magicians whose spells don't always work).
347.6Digital has it nowTLE::REAGANUse only as directedTue Dec 29 1992 13:543
    See also BLISS' MAP declaration.  Just a "cast" with a scoped syntax.
    
    				-John
347.7TLE::REAGANUse only as directedTue Dec 29 1992 13:563
    Also, Apollo's Pascal has real "casts" instead of "coercions".
    
    				-John
347.8Which portable C casts view representation?TLE::AMARTINAlan H. MartinTue Dec 29 1992 15:238
Re .5:

>... in other cases a cast in C will change the value without changing the
>representation.

Offhand, the only such cases I can recall all involve overflow, and are thus
*not* defined by K&R, or ANSI C.
				/AHM/THX