Ada question: shorter records?

I’ve been poking around for a while, and I was working with a part of my project that wanted a two dimensional vector.
I know that my examples aren’t correct syntax but I was trying to do something like
type Vector_2 is ( Float, Float );.
What I’m after is the declaration closest to the above, and assignment that looks like My_Vector_2 : Vector_2 := ( 1.0, 2.0 )
This could also be achieved with type Vector_2 is array(1..2) of Float; and My_Vector_2 : Vector_2 := ( 1.0, 2.0 );

I know the correct way should be something like:

type Vector_2 is record 
   X : Float; -- or even x,y : Float;
   Y : Float;
end record;

My_Vector_2 : Vector_2 := ( 1.0, 2.0 );

I think I already solved my problem while writing this post, but I’m going to put it up anyways to see if I missed anything.
Skipping the naming on something like this is a footgun for sure, though I don’t see why I shouldn’t be able to make a type of two? I’m going to have to make lots of these, is there a way like the first example where I don’t have to name anything, or will I have to just bite it and use the records?

I’ve done something like the following:

type Axis is (X, Y, Z, W);

subtype Axis_2 is Axis range X..Y;

type Vector_2 is array(Axis_2) of Float;

-- define operations for Vector_2...

subtype Axis_3 is Axis range X..Z;

type Vector_3 is array(Axis_3) of Float;

-- define operations for Vector_3...

This is a bit more descriptive since you access a component as V(X) instead of V(1), but IMO it’s a bit too clever. I think in general I’d like to read record syntax instead. However, I often run into cases where it’s convenient to be able to loop over the components of a vector instead of accessing them by name. You just have to pick the tradeoffs you’re comfortable with.

1 Like

How would you access the components if there is no name?

The main difference between record and vector is:

  • Vectors contain elements of the same type
  • Records (can) contain elements of different types

But in general you can do more with vectors (e.g. iteration over the elements, linear algebra), so use vectors when you only have elements of the same type.