Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!ccicpg!turnkey!conexch!enped From: enped@conexch.UUCP (Eric Pederson) Newsgroups: comp.lang.pascal Subject: Re: TP Problem Message-ID: <159@conexch.UUCP> Date: Tue, 1-Dec-87 18:48:32 EST Article-I.D.: conexch.159 Posted: Tue Dec 1 18:48:32 1987 Date-Received: Sat, 12-Dec-87 04:07:54 EST Reply-To: enped@conexch.UUCP (Eric Pederson) Organization: Consultant's Exchange Lines: 71 Keywords: enumerated type error Reference: <158@conexch> In article <1808@rayssdb.RAY.COM> Richard A. Brooks writes: >In article <936@sask.UUCP>, Kevin Lowey writes: >> In article <16249@topaz.rutgers.edu>, Jonathan Joshua writes: >> > >> > [ the preceeding discussion about enumerated type I/O ] >> [...] > Try this: >program TEST; > >type > ON_OFF = (ON,OFF); >var > v = ON_OFF; >begin > readln(v); > writeln('Switch is now ',v); >end. > > Upon compiling the above, you will recieve an I/O not > allowed error at the "readln(v)" and the "writeln(v)", > if the readln is removed. > > TP does NOT allow you to read or write ennumerated types > to the screen!!! Is this so in 4.0?? I know you can change > types, but what a pain. > Right. PASCAL (not just TP) does not define I/O of structured or enumerated types to TEXT files (file of char) which input and output are. This is because the implementation details of these types are supposed to be hidden from the user. The user doesn't neccessarily care what gets written out to a FILE OF anythingelse (that is anything other than char). In addition, if you write a FILE OF anythingelse with an object program compiled by one Pascal compiler, that same file is not neccessarily readable by object programs compiled by different Pascal compilers. For example, VARs of type ON_OFF are (in TP) stored in one byte. The value of that byte is 0 for ON and 1 for off. A common mistake for programmers not used to enumerated types is to assume that "writeln(v)" will produce "ON" or "OFF" on the output file. To accomplish this, the compiler would have to keep the actual character names of every enumerated type in the object program, wasting lotsa space, and complicating the TEXT library I/O routines. For non-TEXT I/O the program will just read and write the single byte. Basically, structured and enumerated types are restricted from TEXT I/O. The workaround is either using ord() to convert the enumerated type to an integer or to write out a TEXTable value based on the enumerated type. TP's structured typed constants make this really easy: type ON_OFF = (ON,OFF); onoffarr = array[ON_OFF] of string[10]; { or [ON..OFF] } const onoffnames: onoffarr = ( 'ON','OFF' ); var v: ON_OFF; begin ... writeln(ord(v),' ',onoffnames[v]); end. --- Eric Pederson HBUHSD 714 964 3339