comp.lang.ada
 help / color / mirror / Atom feed
From: "Mark Lundquist" <mark@rational.com>
Subject: Re: Implementing an elegant range type.
Date: Mon, 19 Mar 2001 18:38:59 GMT
Date: 2001-03-19T18:38:59+00:00	[thread overview]
Message-ID: <7nst6.593482$U46.17851651@news1.sttls1.wa.home.com> (raw)
In-Reply-To: 3ab6442f.0@silver.truman.edu


Chad R. Meiners <crmeiners@hotmail.com> wrote in message
news:3ab6442f.0@silver.truman.edu...
> There are certain times that I have wanted to pass ranges as a
> parameter to subroutines, for example being able to
> write, "Register_Slots (75..4242);" which of course isn't directly
> allowed.  The solution I came up with is to declare a null type that
> doesn't take up any memory so that I can create array type of the
> ranges I want as in the example below.
>
> type Nothing is null record;
> for  Nothing'Size use 0;
>
> type Integer_Range_Type is array (Integer range <>) of Nothing;
>
> Thus I could declare an array, "Integer_Range : Integer_Range_Type
> (Integer);", and use it to pass array slice which would contain the
> range data.  Example: "Register_Slots (Integer_Range(75..4242));"

OK... how does this relate to your subject line, "Implementing an _elegant_
range type"? (emphasis mine) :-) :-) :-)

Looks like you blew past the last "Warning: Entering Kludge Zone" sign a few
miles back! :-)  Aren't you going to extreme lengths just to implement an
"ordered pair" abstraction in a non-idiomatic way?  And why... to be able to
use the ".." notation?  It doesn't seem worth the contortions to me!  Maybe
I'm missing something... if so, please clarify!

Why not just write:

    type Integer_Range is record
        First, Last : Integer;
    end record;

Then you'd be able to say (taking up your example):

    Register_Slots (Integer_Range'(75, 4242));

or

    Register_Slots ((First => 75, Last => 4242));

You can quite easily write

    function Length (Of : Integer_Range);

(or call it "Extent" or whatever) to give the effect of 'Length as you might
have used it with the Integer_Range_Type kludge.

Your Integer_Range_Type solution is kind of clever, but it's not readily
understandable... if I had to deal with some code written with this style of
representing ranges, it would take me a while (even with comments) to figure
out what in the world the Maximum Array Of Nothings is for and why we are
passing around slices of it.  And when I did figure it out, I would be
annoyed :-)

OK, so much for my $.04 (adjusted for inflation :-) worth of unsolicited
advice :-)

>
> The problem I am encountering is that when I declare a variable
> with a range as large as Integer_Range's I get a
> Storage_Error which doesn't make much sense since if Integer_Range's
> range is something smaller than 2**20 elements, Integer_Range'Size
> equals 8 for all cases which I expect should be true even for the
> large cases since the elements of the array have no size.  I am
> using GNAT 3.13p on WinNT.
>
> Has anyone used a similar approach for representing ranges or have
> any advice on getting around the Storage_Error for large ranges?

I don't understand the Size attribute well enough to be of much help here...
I just have not given it enough study.  I don't know of any language rule
that would require GNAT to do what it's doing, but that doesn't mean there
isn't one.  Or maybe it's a GNAT bug.  But most likely it's just something
that the implementation has every right to do.  Like I said, I don't really
know.

But... you might try playing with 'Size of the array type, 'Size of the
array object, and 'Component_Size.  If nothing else, the error messages may
be illuminating.

Mark Lundquist
Rational Software






  reply	other threads:[~2001-03-19 18:38 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-19 17:45 Implementing an elegant range type Chad R. Meiners
2001-03-19 18:38 ` Mark Lundquist [this message]
2001-03-19 20:50   ` Chad R. Meiners
2001-03-22 22:20     ` Nick Roberts
2001-03-23 22:29       ` Brian Rogoff
2001-03-20  4:48   ` Dr Adrian Wrigley
2001-03-20 15:31     ` Robert A Duff
2001-03-21  1:21       ` Dr Adrian Wrigley
2001-03-21  3:58         ` Brian Rogoff
2001-03-22 23:00         ` Array 'downto' [Was: Implementing an elegant range type] Nick Roberts
2001-03-26 18:28           ` Stephen Leake
2001-03-28 22:35           ` Robert A Duff
2001-03-21 22:55 ` Implementing an elegant range type Chad R. Meiners
  -- strict thread matches above, loose matches on Subject: below --
2001-03-20 20:17 Beard, Frank
2001-03-21 13:00 Francisco Javier Loma Daza
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox