标签: dynamic-predicate

我如何强制类型保持只能是一组固定的非连续值?

当我将 3 作为有效的 Scale 值提供给它时​​,我期望这个程序会引发错误,但没有这样的运气:

with Ada.Text_IO; use Ada.Text_IO;

procedure predicate is
   type Scale is new Integer
      with Dynamic_Predicate => Scale in 1 | 2 | 4 | 8;
   GivesWarning : Scale := 3; -- gives warning
begin
   Put_Line ("Hello World");
   loop
      Put_Line ("Gimme a value");
      declare
         AnyValue : Integer := Integer'Value (Get_Line);
         S : Scale := Scale (AnyValue); -- no check done!
      begin
         Put_Line ("okay, that works" & S'Image);
      end;
   end loop;
end predicate;
Run Code Online (Sandbox Code Playgroud)

我发现了这个相关的问题,但要求是使用枚举。解决方案是从枚举 -> 值定义一个数组。 …

ada static-predicate dynamic-predicate

3
推荐指数
1
解决办法
53
查看次数

标签 统计

ada ×1

dynamic-predicate ×1

static-predicate ×1