문제
가 있는 경우 기록이 유형은 다음과 같다:
type ABC is record
A : Integer;
B : Integer;
end record;
어떻게 만들기 하위 ABC 을 가진 두 개의 정형의 범위 지정된?
해결책
에 대한 질문에 대답하지 않지만 (NWS가 말하면서 그렇지 않음), A와 B 대신 정수가되는 경우 배열이었습니다. 다음을 수행 할 수 있습니다.
package Record_Subtypes is
type Int_Data is array (Integer range <>) of Integer;
type ABC (X_Min, X_Max, Y_Min, Y_Max : Integer) is record
A : Int_Data (X_Min .. X_Max);
B : Int_Data (Y_Min .. Y_Max);
end record;
subtype ABC_4_4 is ABC(X_Min => 1, X_Max => 4,
Y_Min => 1, Y_Max => 4);
subtype ABC_1_7_3_12 is ABC (X_Min => 1, X_Max => 7,
Y_Min => 3, Y_Max => 12);
end Record_Subtypes;
.
A 및 B 레코드 필드는 레코드 판별자가 제공 한 것처럼 인덱스 하위 유형을 사용합니다.
이것은 내가 읽은 바이트 수가 고정 크기의 헤더를 통해 제공되는 바이트 수가 제공되는 인터페이스 (예 : 소켓)에서 가변 길이 끈을 읽을 때 유용한 멋진 트릭입니다.또는 열거 형으로 변형 된 변형 기록의 경우 특정 변형에 대한 레코드를 하위 유형으로 하위 테일 수 있습니다.
다른 팁
다음과 같이 일반적인 일반을 사용할 수 있습니다.
generic
type Number is range <>;
package Int_Record is
type ABC is record
A, B : Number;
end record;
end Int_Record;
.
A와 B에 대해 다른 범위를 원할 경우 두 개의 일반 매개 변수를 사용해야합니다.
사용량은 다음과 같습니다 :
procedure Foo is
subtype My_Int is Integer range 1 .. 3;
package My_Int_Record is new Int_Record (Number => My_Int);
X : My_Int_Record.ABC;
begin
X.A := 2; -- okay
X.B := 4; -- error!
end Foo;
. 에 Ada 2012
우리는 지금 Dynamic_Predicate
우리가 할 수 있습을 부과 동등에 대한 제한,다음과 같다:
type ABC is record
A : Integer;
B : Integer;
end record;
subtype XYZ is ABC
with dynamic_predicate =>
((XYZ.A in Positive) and
(XYZ.B not in Positive)) or else raise Constraint_Error;
주어진 :
type ABC is record
A : Integer;
B : Integer;
end record;
.
사용할 수 있습니다 :
type XYZ is record
A : Positive; -- A is subtype of ABC.A
B : Natural; -- B is subtype of ABC.B
end record;
function convert(Input: ABC) return XYZ is
begin
return Result : XYZ:= ( A => Input.A, B => Input.B );
-- Put your exception handling here.
end convert;
. 제휴하지 않습니다 StackOverflow