comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Interresting, possibly buggy behavior in GNAT generics w/ expression function.
Date: Thu, 28 Mar 2013 10:34:43 -0700 (PDT)
Date: 2013-03-28T10:34:43-07:00	[thread overview]
Message-ID: <ac196de8-8d24-4302-8e3b-e0007d691095@googlegroups.com> (raw)

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 was looking into XMLAda (which, oddly fails compile on DOTNET-Gnat and seems oblivious to --target=JVM) so I was tinkering with it in native-code hoping 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 functionality for altering attributes via XMLAda DOM-package (at least nothing obvious).

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 changed from positive to natural. {Actually it only happens when Attribute_Values 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 => Positive,
		Element_Type => String );

    Attribute_Units : Units.Vector:= Units.Empty_Vector;

Package Generic_Attribute_Set is

    Type Attribute(Has_Units : Boolean:= Attribute_Units.Length /= 0) is record
	Value : Attribute_Values;
	case Has_Units is
	When True  => Unit : Positive;
	When False => Null;
	end case;
    end record;

    -- The 'Image Attribute, when applied to numeric types, leaves a leading
    -- 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 => Ada.Strings.Left) );
    
    -- Getting the string stored in the position for out units-vector is a
    -- simple index into the proper position in the vector containing the units.
    Function Image(Value : Positive) Return String is
      ( --if Value not in positive then "" else 
        Units.Constant_Reference( Attribute_Units, Value ) );
    
    Function To_String( Attr : Attribute ) Return String is
      ( Attribute_Name & "=""" & 
	 Image(Attr.Value) & (if Attr.Has_Units then Image(Attr.Unit) else "") &
	 '"'
      );
    
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 => Positive,
		Element_Type => String );

    Use Units;
    Screen : Units.Vector:= Vector'("en" & "ex") & Vector'("px" & "%");
    
    Type J is (Alpha, Beta, 'K', Fred);
    
    Package Margins is new Generic_Attribute_Set(
	Attribute_Name   => "margin-left",
	Attribute_Values => Integer,
	Units            => Units,
	Attribute_Units  => Screen);
Begin
    Declare
      Use Margins;
      K : Attribute;
    begin
	K.Value:= 88; --J'Succ(Beta);
	K.Unit:= 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 there's supposed to be no knowledge of what the parameters are actually instantiated with.


             reply	other threads:[~2013-03-28 17:34 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-28 17:34 Shark8 [this message]
2013-03-28 18:06 ` Interresting, possibly buggy behavior in GNAT generics w/ expression function Simon Wright
2013-03-28 18:38   ` Shark8
2013-03-30  5:59     ` Stephen Leake
2013-03-30 14:20       ` Shark8
2013-03-29  7:45 ` Simon Wright
2013-03-29 12:27   ` Dmitry A. Kazakov
2013-03-29 14:45     ` Simon Wright
2013-03-29 16:21       ` Dmitry A. Kazakov
2013-03-29 16:43         ` Shark8
2013-03-29 17:43         ` Eryndlia Mavourneen
2013-03-29 17:45           ` Eryndlia Mavourneen
2013-03-29 18:38             ` Simon Wright
2013-03-30  0:56         ` Randy Brukardt
2013-03-30  9:24           ` Dmitry A. Kazakov
2013-04-02  0:52             ` Randy Brukardt
2013-04-02  8:51               ` Dmitry A. Kazakov
2013-04-02 21:59                 ` Randy Brukardt
2013-03-29 14:47   ` Shark8
2013-03-29 14:56     ` Shark8
2013-03-30  1:04   ` Randy Brukardt
2013-03-30  2:36     ` Shark8
2013-03-30 15:51     ` Simon Wright
2013-04-01 15:58     ` Adam Beneschan
2013-04-02  0:46       ` Randy Brukardt
2013-04-02  8:18         ` Simon Wright
2013-04-02 11:14         ` Stephen Leake
2013-04-02 22:00           ` Randy Brukardt
2013-04-02 23:01             ` Adam Beneschan
2013-04-04  0:08               ` Randy Brukardt
2013-03-29 14:47 ` Simon Wright
2013-03-29 14:54   ` Shark8
2013-03-29 15:26     ` Bill Findlay
2013-03-29 15:48     ` Simon Wright
replies disabled

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