🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #4287

type changes

#4287
From: Barry_Cornelius
Date: Fri, 17 Jun 1988 08:38
64 lines
3651 bytes
 Nathan Bohlmann and Keith Campbell have recently communicated
 about how type changes are performed in Modula-2.   It is, in fact,
 possible to distinguish between two different kinds of type change
 in Modula-2:
 o  Given a value of one type, the language specifies an appropriate
    value of another type.  This kind of type change will be referred
    to as a coercion.
 o  Given a value of one type, its internal representation is interpreted
    as if it were that of another type.  This kind of type change will
    be referred to as a cast.
 Often an implementation will have to generate run-time code
 in order to perform a coercion, whereas a cast requires no such
 overhead.  Obviously, casts should be avoided if a program is to be
 portable.  Sometimes coercions and casts are referred to as
 safe and unsafe conversions.

 In Modula-2, there are five standard functions
 which perform coercions; they are CHR, ORD, FLOAT, TRUNC and VAL.

 Section 12 of the Modula-2 Report includes details of how casts
 can be performed in Modula-2.  A type identifier T can be used
    ``as a name denoting the type transfer from the type
    of the operand to the type T.  ... such functions
    ... involve no explicit conversion instructions''.
 Although it is not actually stated, implementations usually assume
 that the values of the two types occupy the same amount of storage.
 For example, suppose that this is true for the types CARDINAL
 and INTEGER and that the CARDINAL variable c has the value
 65535, then the expression INTEGER(c) is of type INTEGER
 and has a value which is dependent on the internal representations of
 the types CARDINAL and INTEGER.  This value is often -1
 on a 16-bit computer and 65535 on a 32-bit computer.

 Programmers frequently use expressions like INTEGER(c)
 in order to convert a value of type CARDINAL into one of type
 INTEGER.  However, this practice ought to be avoided
 since it only works if the types CARDINAL and INTEGER
 have the same representation for the possible values of c.
 Although this is likely to be the case, programmers ought to use
 VAL(INTEGER,c) which is guaranteed to work for appropriate values of c.

 It is interesting to note that most of the problems concerning
 type changes are not present in the language Oberon.  In particular,
 the type CARDINAL has been removed and the types LONGREAL, REAL,
 LONGINT, INTEGER and SHORTINT are compatible with each other.
 Wirth also considers it inappropriate to define for Oberon
 a mechanism for performing casts.  He describes Modula-2's
 type transfer functions as a ``a particularly seducing trap''!
 ==
 Barry Cornelius
 ==
 Address:
    Computer Science Group, School of Engineering and Applied Science,
    University of Durham, Durham, DH1 3LE, England
 Telephone:
    My office: Durham (091 or +44 91) 374 2638
    Secretary: Durham (091 or +44 91) 374 2630
    Fax:       Durham (091 or +44 91) 374 3740
 Electronic Mail Addresses:
    JANET:       Barry_Cornelius@uk.ac.dur.mts
    Internet:    Barry_Cornelius%mts.dur.ac.uk@cunyvm.cuny.edu
    UUCP:        ...ukc!cs.nott.ac.uk!bjc
    BITNET/EARN: Barry_Cornelius%DUR.MTS@AC.UK
Technical Details
Message-ID: <INFO-M2%88061707562183@UCF1VM>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!husc6!uwvax!oddjob!ncar!ames!pasteur!ucbvax!durham.ac.UK!Barry_Cornelius

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!husc6!uwvax!oddjob!ncar!ames!pasteur!ucbvax!durham.ac.UK!Barry_Cornelius
From: Barry_Cornelius@durham.ac.UK
Newsgroups: comp.lang.modula2
Subject: type changes
Message-ID: <INFO-M2%88061707562183@UCF1VM>
Date: 17 Jun 88 08:38:57 GMT
Sender: daemon@ucbvax.BERKELEY.EDU
Reply-To: Info-Modula2 Distribution List <INFO-M2%UCF1VM.bitnet@jade.berkeley.edu>
Organization: The Internet
Lines: 64