I have a vector of the form (x,y,x) representing coordinates. I want to be able to do something like (x,y,z) + (x2,y2,z2) to produce a new set of coordinates. Ada says it cant use '+' for composite types, but surely there is a way I can do this?
Asked
Active
Viewed 293 times
2
-
3How did you define your vector? Please read thIs: [mre]. – Keith Thompson Sep 18 '20 at 02:27
-
3And be sure to show us the exact error message in your question. Ada certainly can apply `+` to composite types if you define it properly. – Keith Thompson Sep 18 '20 at 04:30
2 Answers
6
If you have
type Vector is record
X : Float;
Y : Float;
Z : Float;
end record;
you can define +
as
function "+" (L, R : Vector) return Vector is
(L.X + R.X, L.Y + R.Y, L.Z + R.Z);
Be careful when you define -
similarly to use -
throughout! that error is very hard to spot.

Simon Wright
- 25,108
- 2
- 35
- 62
3
If you define your vectors to contain elements of a floating point type you can use the generic package Ada.Numerics.Generic_Real_Arrays. This package is described in the Ada Language Reference Manual section G.3.1.
If you want to define your vectors to contain elements of a complex number type then you can use the generic package described in section G.3.2 Complex Vectors and Matrices
If you wish to use integer types as your vector components you can write the "+" function for your integer vector type.

Jim Rogers
- 4,822
- 1
- 11
- 24