From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f30ef262af690ce0 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!feeder.news-service.com!newsfeed.freenet.de!news.tu-darmstadt.de!newsfeed.velia.net!news-fra1.dfn.de!storethat.news.telefonica.de!telefonica.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: numbers as 'generics' parameters Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <482dd91a$1_3@news.bluewin.ch> <1g4p2jgvi3k8j.1j2xk6p9ws3a2.dlg@40tude.net> <9e99c5cd-fd94-4a38-89fd-681c0372b7ad@y38g2000hsy.googlegroups.com> Date: Sun, 18 May 2008 16:52:32 +0200 Message-ID: <1nng3xn67do3n$.1f4isrrr1y1m2.dlg@40tude.net> NNTP-Posting-Date: 18 May 2008 16:52:33 CEST NNTP-Posting-Host: 4a4a00b3.newsspool1.arcor-online.net X-Trace: DXC=7<;leKbk;cMWDmlTRbh@=Iic==]BZ:afN4Fo<]lROoRA<`=YMgDjhgBCK5bF[H?U\C[6LHn;2LCVN[H On Sun, 18 May 2008 06:58:03 -0700 (PDT), Maciej Sobczak wrote: > On 17 Maj, 19:34, "Dmitry A. Kazakov" > 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 > void foo(const fixed_size_string & s1, > const fixed_size_string & 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