Ada:变体记录,其中一部分案例共享一个共同的属性

时间:2012-10-06 18:30:54

标签: ada

我有以下代码,我只想在前两个案例中共享共同财产;但是,当我尝试使用这种语法时,我收到错误"id" conflicts with the declaration at line 11

   type Shape (Which : Shape_Type := SQUARE) is
      record
      case Which is
         when Square =>
            id : Natural;   -- Line 11
         when Turnout =>
            id : Natural;   -- Line that causes error to be thrown
         when Invalid =>
            null;
      end case;
      end record;

1 个答案:

答案 0 :(得分:5)

此:

type Shape (Which : Shape_Type := SQUARE) is
   record
      case Which is
         when Square | Turnout =>
            id : Natural;
         when Invalid =>
            null;
      end case;
   end record;

如果您以后希望Turnout案例具有额外属性,则可以使用嵌套case来执行此操作(但您仍需要涵盖所有备选方案):

type Shape (Which : Shape_Type := SQUARE) is
   record
      case Which is
         when Square | Turnout =>
            id : Natural;
            case Which is
               when Square =>
                  null;
               when Turnout =>
                  Deg : Natural;
               when Invalid =>
                  null;
            end case;
         when Invalid =>
            null;
      end case;
   end record;