home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11714 < prev    next >
Encoding:
Text File  |  1992-07-27  |  2.6 KB  |  84 lines

  1. Xref: sparky comp.lang.c++:11714 comp.std.c++:986
  2. Path: sparky!uunet!darwin.sura.net!mips!think.com!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
  3. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  4. Newsgroups: comp.lang.c++,comp.std.c++
  5. Subject: tagged unions, an alternative to RTTI (Re: run-time type checking)
  6. Message-ID: <TMB.92Jul29125322@arolla.idiap.ch>
  7. Date: 29 Jul 92 16:53:22 GMT
  8. References: <1992Jul24.234628.21196@cadsun.corp.mot.com>
  9.     <1992Jul25.172045.24675@ucc.su.OZ.AU> <BryL9q.K5I@watcgl.waterloo.edu>
  10.     <1992Jul28.183746.24287@ucc.su.OZ.AU>
  11. Sender: news@ai.mit.edu
  12. Reply-To: tmb@idiap.ch
  13. Followup-To: comp.lang.c++
  14. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  15.     Perceptive)
  16. Lines: 65
  17. In-reply-to: maxtal@extro.ucc.su.OZ.AU's message of 28 Jul 92 18:37:46 GMT
  18.  
  19. In article <1992Jul28.183746.24287@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  20.  
  21.    >Consider the classic "List" class and "ListNode" class.
  22.    >ListNode is a base class, and assume there are two flavors
  23.    >of derived node.  We write a List that contains a mix of
  24.    >these nodes out to a file; now we want to read the List back
  25.    >in and reconstruct it.
  26.    >
  27.    >Can it be done without resorting to an isA() member function,
  28.    >some member data to identify node type, and possibly some
  29.    >casting/conversion?
  30.  
  31.        a) without some type information, it cant be done.
  32.        b) without casting it can (and should :-) be done
  33.  
  34.        union hetero {
  35.            type1 *p1;
  36.            type2 *p2;
  37.        };
  38.  
  39.  
  40.    And the list is homogeneous--all the elements are of type 'hetero'.
  41.  
  42. It would, in fact, be nice if C++ had a tagged union type, like Pascal
  43. or SML. Something along the lines of:
  44.  
  45. union hetero int {
  46. case 1: type1 *p1;
  47. case 2: type2 *p2;
  48. };
  49.  
  50. int f(hetero h) {
  51.     switch(h) {
  52.     case 1: return h.p1->foo();
  53.     case 2: return h.p2->bar();
  54.     }
  55. }
  56.  
  57. void g(hetero &h) {
  58.     // set the tag of "h" to "1" and the value of p1 to NULL
  59.     h.p1 = 0;    
  60. }
  61.  
  62. The members of tagged unions should usually only be used inside a
  63. switch statement (note that switching on a tagged union is a new
  64. construct, distinct from switching on an integral type).  Updates of
  65. one of the members would automatically update the type tag.  There are
  66. probably some additional syntactic and semantic issues to be resolved.
  67.  
  68. I think such a facility would be very useful and have some advantages
  69. over RTTI:
  70.  
  71.  * works for any collection of types (objects don't have to have
  72.    virtual function tables, etc.)
  73.  
  74.  * the compiler can warn about type cases that the programmer
  75.    has forgotten to handle in his "switch" statement
  76.  
  77.  * tagged unions would allow objects with constructors/destructors
  78.    in unions
  79.  
  80.  * easy to implement and explain
  81.  
  82.                     Thomas.
  83.  
  84.