I have the following -simple- problem: A delta-type for representation of degrees and a subtype of it, angles, where I limted the range from 0.0 .. <360.0. Now, I want to override the "+"-operator for the subtype to have my own modulo-operator:
package ...
type Degree is digits Degree_Digits;
end package
package ...
subtype Limited_Angle is Degree range Degree_Min .. Degree_Max;
function "+" (Left, Right : in Limited_Angle) return Limited_Angle;
end package
And the implementation:
function "+" (Left, Right : in Limited_Angle) return Limited_Angle is
res : Degree;
begin
res := Units.Base.Degrees."+"(Left, Right);
...
return Limited_Angle(res);
end "+";
But I do not like the way, the +-operator is called. My first idea was to have something like
res := Degree(Left) + Degree(Right);
But this does not work. My compiler is warning about infinite recursion. Even the more strict:
res := (Degree(Left)) + (Degree(Right));
is warning about infinite recursion. And I do not understand the principle behind this. Shouldn't T(S) transform S to T? It can't be an optimization problem since (T(S)) does also not work.
Did I miss-understand the concept of type-subtype conversion (most probably) and does anyone have a solution/explanation? Or even a better solution?
Thanks!
A subtype is a type together with a constraint; a value is said to belong to a subtype of a given type if it belongs to the type and satisfies the constraint; the given type is called the base type of the subtype.
Language-Defined Types The principal scalar types predefined by Ada are Integer , Float , Boolean , and Character . These correspond to int , float , bool / boolean , and char , respectively.
Your problem isn't related to type conversion, but to understanding the difference between types and subtypes.
All subtypes of a type have exactly the same operations. The differences between subtypes of a type (including the type itself, technically the "first subtype of the type") is only the set of allowed values.
This means that all your conversions between Degrees
and Limited_Degrees
don't do much, and your call to Units.Base.Degrees."+" (Left, Right)
is actually a recursive call.
I think the solution to your problem is to make Limited_Degrees
a derived type, and not a subtype.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With