【问题标题】:Ada: How to enumerate a type that consists of integers and other types?Ada:如何枚举由整数和其他类型组成的类型?
【发布时间】:2016-12-30 11:21:08
【问题描述】:

例如,我想创建一个代表所有牌组的类型(即 2-10,Jack、Queen、King 和 Ace)。

我想过这样做:

    type Rank is (2,3,4,5,6,7,8,9,10,Jack,Queen,King,Ace);

但我收到此错误:

    identifier expected

【问题讨论】:

    标签: types ada


    【解决方案1】:

    你不能。

    枚举类型声明中的列表由标识符和/或字符文字组成。在那种情况下,你不能有整数文字。

    您可以使用表示子句指定用于表示枚举数的值,但我认为这不是您想要的。

    只使用标识符:

    type Rank is (R2,R3,R4,R5,R6,R7,R8,R9,R10,Jack,Queen,King,Ace);
    

    【讨论】:

    • 感谢您的精彩回答。
    【解决方案2】:

    您可以声明两种辅助类型和组合的一种:

    package Mixed_Enumeration_And_Integer is
       type Integer_Values is range 1 .. 10;
       type Enumeration_Values is (Jack, Queen, King, Ace);
    
       type Object is private;
    
       function "+" (Item : Integer_Values) return Object;
       function "+" (Item : Enumeration_Values) return Object;
    
       function "+" (Item : Object) return Integer_Values;
       function "+" (Item : Object) return Enumeration_Values;
    
       function "=" (Left  : Integer_Values;
                     Right : Object) return Boolean;
       function "=" (Left  : Enumeration_Values;
                     Right : Object) return Boolean;
    
    private
       type States is (Uninitialized, Integer, Enumeration);
    
       type Object (State : States := Uninitialized) is
          record
             case State is
                when Uninitialized => null;
                when Integer       => I : Integer_Values;
                when Enumeration   => E : Enumeration_Values;
             end case;
          end record;
    end Mixed_Enumeration_And_Integer;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多