Path: utzoo!utgpu!water!watmath!clyde!rutgers!rochester!cornell!uw-beaver!mit-eddie!husc6!ncar!ames!pacbell!vixie!paul From: paul@vixie.UUCP (Paul Vixie Esq) Newsgroups: comp.lang.modula2 Subject: Re: union types Message-ID: <850@vixie.UUCP> Date: 27 Mar 88 20:51:01 GMT References: <8803221947.AA00248@nrl-iws6.ARPA> <4118@cup.portal.com> Reply-To: paul@vixie.UUCP (Paul Vixie Esq) Organization: Vixie Enterprises, San Francisco Lines: 51 In article <4118@cup.portal.com> R_Tim_Coslet@cup.portal.com writes: ##Can anyone construct a suitable translation of: ## ## struct obj_name { ## int obj_type; ## union { ## int id; ## char *path; ## } obj_id; ## }; # #The direct equivalent of the above C in Modula-2 is.... # # ObjName : RECORD # ObjType : INTEGER; # CASE : BOOLEAN OF # TRUE : id : INTEGER | # FALSE : path : POINTER TO CHAR | # END # END # # #This should creat the same data structure (and actually has one less #identifier: obj_id). Sometimes you *want* that intervening obj_id. In C, it's harder (though possible) to make a variant record where this intervening member needn't be named in references to the variant fields; in M2, you can do it thus: TYPE ObjName = RECORD (* note 1 *) ObjType: INTEGER; ObjId: RECORD CASE BOOLEAN OF (* note 2 *) TRUE: id: INTEGER| FALSE: path: POINTER TO CHAR; (* note 3 *) END END END; Note 1: we are creating a type in the C example, not a variable. Note 2: No ':' before the type as far as I know; [brackets] may be needed (I don't recall), and the type could be enumerated if more than two variants are needed -- BOOLEAN is convenient but not mandatory. Note 3: POINTER TO CHAR is one way to represent strings, but sometimes arrays are used. Sure would be great if open arrays were allowed in places other than a formal argument on a procedure... -- Paul A Vixie Esq paul%vixie@uunet.uu.net {uunet,ptsfa,hoptoad}!vixie!paul San Francisco, (415) 647-7023