Re: Records that could be arrays



Justin Gombos <rpbkbq.xax.gld@xxxxxxxx> writes:

I noticed this example code in 5.4.4 of the Ada Quality and Style
Guide:

type Coordinate is
record
Row : Local_Float;
Column : Local_Float;
end record;

type Window is
record
Top_Left : Coordinate;
Bottom_Right : Coordinate;
end record;

Would anyone here write something like that?

Yes; Windex has it. Although if I was rewriting it from scratch now,
I might use an array.

They are trying to illustrate that records should not always be flat,
which is a fine example for that purpose, but this seems to set a poor
example. Does anyone see a reason to use a record when an array can
be used? My version of the same structure would look more like:

type Axis is (Row, Column);
type Window_Vertex is (Top_Left, Bottom_Right);

type Coordinate is array (Axis) of Local_Float;
type Window is array (Window_Vertex) of Coordinate;

I have set a rule for myself: Composite types composed solely of one
type of element should be declared as arrays rather than records.
I've never seen this rule in a coding standard. The idea is that you
can be more expressive with an array. Example- there are more options
when it comes to an arrays role in control structures. Plus the
"others =>" notation is available. Thoughts?

I usually lean towards arrays, partly for the reasons you give, partly
because I have library utilities that work much more nicely with
arrays than records; Text_IO and containers for example. I can
create a Text_IO procedure Put for an array with a simple
instantiation; for a record, I have to use auto_text_io.

But records are deeply embedded in my psyche; these types just feel
more natural as records than arrays. I guess we need to catch the kids
in high school, and teach them to use arrays instead of records.

--
-- Stephe
.



Relevant Pages

  • Re: trouble creating array of objects
    ... >1 reason he might not want to use vectors is perhaps because he isn't ready ... >Or perhaps he is simply learning about Arrays. ... there is a learning curve to telling newbie's to ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: trouble creating array of objects
    ... >>1 reason he might not want to use vectors is perhaps because he isn't ... >>Or perhaps he is simply learning about Arrays. ... A programmer should at least know and understand templates before they use ...
    (alt.comp.lang.learn.c-cpp)
  • Re: [PATCH 1/2] avoid OPEN_MAX in SCM_MAX_FD
    ... In git, I used PATH_MAX, and the reason ... I want a constant for arrays ... to some paper standard". ... realize that modern distributions don't put the kernel headers in their ...
    (Linux-Kernel)
  • Re: trouble creating array of objects
    ... > about dynamically multidimensinoal arrays is described. ... it's an excellent learning excercise. ... This is called the C++ standard template library and it is an addition to ... >> Another reason might be the overheads incurred. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: about sprintf function
    ... > will write to some memory locations beyond 'str' and corrupt them. ... that is *not* the reason the result is different. ... even if both str and append contain 2 character strings. ... You need 13 byte long arrays if you want to fit "some__string" ...
    (comp.lang.c)