comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: What would be the Ada solution?
Date: Fri, 04 Feb 2011 12:47:52 -0500
Date: 2011-02-04T12:47:52-05:00	[thread overview]
Message-ID: <wcczkqbwv1z.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 635897a9-13a6-409c-948d-37d93c5e1c29@glegroupsg2000goo.googlegroups.com

mockturtle <framefritti@gmail.com> writes:

> A solution that I saw used somewhere
>
>    type Extended_Values is new integer range -1 .. 1023;
>    subtype Valid_Values is Extended_Values range Extended_Values'first + 1 .. Extended_values'last;
>  
>    No_Value : constant Extended_Values := Extended_Values'first;
>
> A variable that should always have a valid value is declared of type
> Valid_Values and you should get an exception when you try to assign
> No_Value to it.

Yes.  Or:

    type Valid_Values is range 0 .. 2**10 - 1;
    subtype Extended_Values is Valid_Values'Base range -1 .. Valid_Values'Last;

The base range is guaranteed to contain -1, because base ranges are
always symmetric about zero (usually with one extra negative value).

But these solutions involve implicit run-time checks.
Using two types instead of subtypes moves most of the
checking to compile time.

- Bob



  reply	other threads:[~2011-02-04 17:47 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-04 16:33 What would be the Ada solution? KK6GM
2011-02-04 16:38 ` Mark Lorenzen
2011-02-04 16:57 ` Robert A Duff
2011-02-04 18:59   ` Georg Bauhaus
2011-02-04 19:33     ` Robert A Duff
2011-02-04 17:14 ` mockturtle
2011-02-04 17:47   ` Robert A Duff [this message]
2011-02-04 17:26 ` Dmitry A. Kazakov
2011-02-04 17:38 ` Robert A Duff
replies disabled

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