根据 https://github.com/AdaCoreU/Courses/blob/master/lectures/03_Programming_in_the_Large/02_Type_Safety/slides/Strong_Typing.ppt?raw=true 的幻灯片 28,下面的代码是正确的,因为“T 是 Integer 的子类型.因此,V1和V2属于同一类型"
procedure weirdada is
subtype T is Integer range 1 .. Integer'Last;
V1 : Integer := 0;
V2 : T := V1;
begin
null;
end;
但是如果我被允许违反范围声明的目的是什么?我的想法似乎是正确的,因为在编译时有警告,在运行时有异常。
$ ./gnat-gpl-2014-x86-linux-bin/bin/gnatmake weirdada.adb
gcc -c weirdada.adb
weirdada.adb:4:19: warning: value not in range of type "T" defined at line 2
weirdada.adb:4:19: warning: "Constraint_Error" will be raised at run time
gnatbind -x weirdada.ali
gnatlink weirdada.ali
$ ./weirdada
raised CONSTRAINT_ERROR : weirdada.adb:4 range check failed
幻灯片不正确,还是我误解了什么?