Oneof



next up previous contents index
Next: Maybe Up: No Title Previous: Struct

Oneof

"Oneof"s are immutable tagged objects. An instantiation of the "oneof" type provides the name and associated type of each possible tag for the "oneof" objects belonging to that type; there must be at least one tag, and the tag names must all be distinct. Each object of the type has one of these tags, and a value of the associated type. Methods exist to determine the tag and value of a "oneof" object, but usually "oneof"s are decomposed using the tagcase statement (8.11).

"Oneof"s are created using constructors. Associated with each "oneof" type there is a set of constructors, one for each tag of the type. Here is an example:

ot = oneof[some: int, none: null]  % a oneof type
x: ot                              % a variable to denote objects of this type
x := ot{none: nil}                 % creating an object with the none tag

if x.is_none( ) then               % checking the tag
   x := ot{some: 7}                % creating an object with the some tag
   end

tagcase x                          % decomposing a oneof using the tagcase statement
   when none: ...
   when some(y: int): ...
   end

Methods for "oneof" type "ot"

  is_a ( ) returns (bool)
      % (here a is a tag name and T is the corresponding type)
      % effects   returns true if the tag of self is a else returns false

  value_a ( ) returns (T) signals (wrong_tag)
      % (here a is a tag name and T is the corresponding type)
      % effects   if the tag of self is a returns the associated object else signals wrong_tag

  equal (x: ot) returns (bool)
        where all field types T have equal (T) returns (bool)
      % effects   returns true if self and x have the same tag and equal values (determined
      %           by calling the equal method for the value).

  similar (x: ot) returns (bool) 
        where all field types T have similar (T) returns (bool)
      % effects   returns true if self and x have the same tag and similar values (determined
      %           by calling the similar method for the value).
    
  copy ( ) returns (ot)
        where all field types T have copy ( ) returns (T)
      % effects   returns a new oneof object with the same tag as self and whose value is a
      %           copy (obtained by calling the value's copy method) of that of self

  unparse ( ) returns (string)
        where all field types T have unparse ( ) returns (string)
      % effects   returns a string representing the tag and value of self.  The form of the string
      %           is oneof{t: v}, where t is a string corresponding to the current tag and v is
      %           produced by calling the unparse method of the value



theta-questions@lcs.mit.edu