2

我有以下代码,我只希望前两种情况共享一个公共属性;但是,"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;
4

1 回答 1

5

这:

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

如果您以后希望Turnoutcase 有一个额外的属性,您可以使用嵌套来做到这一点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;
于 2012-10-06T19:32:20.860 回答