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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.224.110.68 with SMTP id m4mr49369qap.2.1364492084380; Thu, 28 Mar 2013 10:34:44 -0700 (PDT) X-Received: by 10.50.53.232 with SMTP id e8mr2159861igp.14.1364492084160; Thu, 28 Mar 2013 10:34:44 -0700 (PDT) Path: num2.nntp.dca.giganews.com!num1.nntp.dca.giganews.com!number.nntp.dca.giganews.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!ca1no14890231qab.0!news-out.google.com!v17ni9qad.0!nntp.google.com!ca1no14890214qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 28 Mar 2013 10:34:43 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.20.190.126; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC NNTP-Posting-Host: 69.20.190.126 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Interresting, possibly buggy behavior in GNAT generics w/ expression function. From: Shark8 Injection-Date: Thu, 28 Mar 2013 17:34:44 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: number.nntp.dca.giganews.com comp.lang.ada:180821 Date: 2013-03-28T10:34:43-07:00 List-Id: Lately I've been interested in perhaps getting Ada as a script-tag language= , much like JavaScript. I figured I'd need a DOM library or somesuch and wa= s looking into XMLAda (which, oddly fails compile on DOTNET-Gnat and seems = oblivious to --target=3DJVM) so I was tinkering with it in native-code hopi= ng to be able to get to a point where I could seek-out and alter attributes= [for a node] on a DOM-Document and was surprised to find there's no functi= onality for altering attributes via XMLAda DOM-package (at least nothing ob= vious). So I decided to play around and see about my own DOM-library (which I would= rather not do, as there are several DOM-ish Ada packages about) and found = this interesting tidbit: behavior changes when the parameter of image is ch= anged from positive to natural. {Actually it only happens when Attribute_Va= lues is instantiated with Integer[-type].} ---------------------------------------------------------------------------= - With Ada.Containers.Indefinite_Vectors, Ada.Strings.Fixed; Use Ada.Containers; Generic Attribute_Name : String; Type Attribute_Values is (<>); With Function Img(Value : Attribute_Values) Return String is Attribute_= Values'Image; With Package Units is New Indefinite_Vectors( Index_Type =3D> Positive, Element_Type =3D> String ); Attribute_Units : Units.Vector:=3D Units.Empty_Vector; Package Generic_Attribute_Set is Type Attribute(Has_Units : Boolean:=3D Attribute_Units.Length /=3D 0) i= s record Value : Attribute_Values; case Has_Units is When True =3D> Unit : Positive; When False =3D> Null; end case; end record; -- The 'Image Attribute, when applied to numeric types, leaves a leadin= g -- space for non-negative numbers (for uniform columnuar display); this= is -- undesirable for our use, so we wrap it in a call to trim. Function Image(Value : Attribute_Values) Return String is ( Ada.Strings.Fixed.Trim(Img(Value), Side =3D> Ada.Strings.Left) ); =20 -- Getting the string stored in the position for out units-vector is a -- simple index into the proper position in the vector containing the u= nits. Function Image(Value : Positive) Return String is ( --if Value not in positive then "" else=20 Units.Constant_Reference( Attribute_Units, Value ) ); =20 Function To_String( Attr : Attribute ) Return String is ( Attribute_Name & "=3D""" &=20 Image(Attr.Value) & (if Attr.Has_Units then Image(Attr.Unit) else "") & '"' ); =20 End Generic_Attribute_Set; ---------------------------------------------------------------------------= - With Generic_Attribute_Set, Ada.Streams, Ada.Text_IO.Text_Streams, Ada.Containers.Indefinite_Vectors; Procedure Test is Package Units is New Ada.Containers.Indefinite_Vectors( Index_Type =3D> Positive, Element_Type =3D> String ); Use Units; Screen : Units.Vector:=3D Vector'("en" & "ex") & Vector'("px" & "%"); =20 Type J is (Alpha, Beta, 'K', Fred); =20 Package Margins is new Generic_Attribute_Set( Attribute_Name =3D> "margin-left", Attribute_Values =3D> Integer, Units =3D> Units, Attribute_Units =3D> Screen); Begin Declare Use Margins; K : Attribute; begin K.Value:=3D 88; --J'Succ(Beta); K.Unit:=3D 4; Ada.Text_IO.Put_Line( To_String(K) ); end; End Test; ---------------------------------------------------------------------------= - As I understand it this shouldn't *ever* happen because in the generic ther= e's supposed to be no knowledge of what the parameters are actually instant= iated with.