comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: numbers as 'generics' parameters
Date: Sun, 18 May 2008 16:52:32 +0200
Date: 2008-05-18T16:52:33+02:00	[thread overview]
Message-ID: <1nng3xn67do3n$.1f4isrrr1y1m2.dlg@40tude.net> (raw)
In-Reply-To: dda74cc4-302b-43fd-9aa0-16fda165a48f@y38g2000hsy.googlegroups.com

On Sun, 18 May 2008 06:58:03 -0700 (PDT), Maciej Sobczak wrote:

> On 17 Maj, 19:34, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
> wrote:
> 
>> How are going to check string length statically?
> 
> That's what I'm asking. OP asked about numbers in generics, motivating
> his question with compile-time checks. Surely he comes from C++ and he
> was later flooded with answers that focused on numbers, generics and
> alternatives, but the "compile-time" part of the discussion was
> immediately lost.
>
> I can bet that OP was asking about this:
> 
> template <size_t N>
> void foo(const fixed_size_string<N> & s1,
>          const fixed_size_string<N> & s2)
> {
>     // some processing here
> }
> 
> fixed_size_string<3> s = "Ada";
> fixed_size_string<3> t = "C++";
> fixed_size_string<4> u = "Java"
> 
> foo(s, t); // OK, s and t have statically equal lengths
> foo(s, u); // compile-time error *here*

generic
   N : Natural;
package Strings_Nobody_Ever_Needed is
   type Pascal_String is new String (1..N);
   procedure Foo (L, R : Pascal_String);
end Strings_Nobody_Ever_Needed;

In your example string length is static. So it could be enforced
statically. When you asked for a procedure which enforces same constraint
on two arguments, that is a completely different thing, from what your
example presents. You do not have *one* procedure there.

If you replaced the type declaration in Strings_Nobody_Ever_Needed to

   subtype Pascal_String is String (1..N);

you would start to see what's wrong with all this... (:-))

Talking about strings, they are allowed to have statically unknown length,
so a request to check it statically is meaningless and wrong:

declare
   X : String (1..40);
begin
   X := Y;  -- This *shall* compile and legal no matter of Y'Length
exception
   when Constraint_Error =>
      if Y'Length > X'Length then
         X := Y (Y'First..Y'First + X'Length - 1);
      else
         X (X'First..X'First + Y'Length - 1) := Y;
      end if;
end;

> Yes, Ada allows to use numbers in types - in various ways.
> But the OP's question is still not directly addressed.

His question was answered. It is possible to have formal objects in
generics. Each instantiation produces new instance of everything inside, so
it is statically different from any other instance, which is a drawback,
not an advantage. If instances need to bound over a parameter, that is
possible to do as I have showed in my earlier post. Differently to C++ it
has to be done explicitly because matching the instances is named in Ada
(by structure in C++), which is an advantage, not a drawback...

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2008-05-18 14:52 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-16 17:26 numbers as 'generics' parameters jhc0033
2008-05-16 17:55 ` Adam Beneschan
2008-05-16 19:12   ` John B. Matthews
2008-05-16 18:34 ` jimmaureenrogers
2008-05-16 18:57 ` Gautier
2008-05-16 20:52   ` Maciej Sobczak
2008-05-16 23:09     ` Peter C. Chapin
2008-05-17  7:47     ` Dmitry A. Kazakov
2008-05-17 16:29       ` Maciej Sobczak
2008-05-17 17:34         ` Dmitry A. Kazakov
2008-05-18 13:58           ` Maciej Sobczak
2008-05-18 14:52             ` Dmitry A. Kazakov [this message]
2008-05-18 20:37               ` Maciej Sobczak
2008-05-19  9:02                 ` Dmitry A. Kazakov
2008-05-19  5:16             ` Ivan Levashew
2008-05-19  8:26               ` Maciej Sobczak
2008-05-16 20:22 ` Jeffrey R. Carter
2008-05-21 15:32   ` 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