comp.lang.ada
 help / color / mirror / Atom feed
* Pure vs. Pure; aspect vs. pragma.
@ 2013-03-29  0:32 Shark8
  0 siblings, 0 replies; only message in thread
From: Shark8 @ 2013-03-29  0:32 UTC (permalink / raw)


In the Ada 2012 rationale it says:
>  A number of existing pragmas are paralleled by aspect specifications but the pragmas are not made obsolete. Examples are the pragmas relating to packages such as Pure, Preelaborate, Elaborate_Body and so on.
>
> Thus we can write either of
>
>   package P is
>     pragma Pure(P);
>   end P;
>
> or
>
>   package P
>   with Pure is
>   end P;

So they should be equivalent; this, however, is not so in GNAT-2012.


Example:
-----------------------------------------------
-- Test.adb
Pragma Ada_2012;
Pragma Assertion_Policy( Check );

With
some_function,
subtype_definitions,
subtype_definitions2,
Ada.Text_IO;

Procedure Test is

Function F( Parameter : Integer ) Return Integer renames some_function;
Begin
    Ada.Text_IO.Put_Line("Starting Test:");

    declare
	Generic
	    Type S is (<>);
	    With Function Image( Item: S ) Return String is S'Image;
	Package Metric is

	    First : Constant S :=  S'First;
	    Last  : Constant S :=  S'Last;
	    
	    Function Range_String(Name : String) Return String is
		    (Name & " is Range" & First'Img & " .." & Last'Img);
	End Metric;
	
	
	
	-- Swap out these definitione to see the differences.
	Package S is new subtype_definitions;
--  	Package S is new subtype_definitions2(f(1), f(2), f(11));
	
	Package M1 is new Metric( S.S1 );
	Package M2 is new Metric( S.S2 );
	Package M3 is new Metric( S.S3 );
	
	Use Ada.Text_IO;
    begin
	Put_Line( M1.Range_String( "S1" ) );
	Put_Line( M2.Range_String( "S2" ) );
	Put_Line( M3.Range_String( "S3" ) );
    end;


    Ada.Text_IO.Put_Line("Testing complete.");
End Test;
-----------------------------------------------
-- some_function.adb
With Ada.Numerics.Discrete_Random;

Function Some_Function( Parameter : Integer ) Return Integer is
    Subtype K is Positive Range 64..1024;
    Package RNG_Pkg is new Ada.Numerics.Discrete_Random(Result_Subtype => K);
    RNG : RNG_Pkg.Generator;
begin
    
    -- There is actually another function here importing glGetIntegerv,
    -- executing it, and returning the result in 'Result'.
    Return Result : Integer do
	for Index in 1..Parameter loop
	    Result:= RNG_Pkg.Random(RNG);
	end loop;
    end return;
end Some_Function;
-----------------------------------------------
-- subtype_definitions.ads
With some_Function;

generic
package subtype_definitions with Pure is
    -- Cannot uncomment.
    -- Pragma Pure;
    
    subtype S1 is Integer Range
      0 .. some_function(1);

    subtype S2 is Integer Range
      0 .. some_function(2);

    subtype S3 is Integer Range
      0 .. some_function(11);

end subtype_definitions;
-----------------------------------------------
-- subtype_definitions2.adb
generic
    L1 : Integer;
    L2 : Integer;
    L3 : Integer;
package subtype_definitions2 with Pure is
    Pragma Pure;
    
    subtype S1 is Integer Range
      0 .. L1;
    
    subtype S2 is Integer Range
      0 .. L2;
    
    subtype S3 is Integer Range
      0 .. L3;

end subtype_definitions2;

---------------------------------------------------

I ran into this when restructuring my open-gl library; the idea being that I can have the object-enumerations (lights, buffers, etc) definitions dynamically defined at instantiation on the target computer -- these enumerations have certain mandatory minimums [on the upper bound] and those, it seems, are what the open-gl bindings I've looked at [mostly C/C++] seem to embrace... but it seems a bit limiting to do that in Ada when we have subtypes [which can be dynamically set].

{Of course subprograms taking those enumerations should have parameters of those subtypes; leading me to think generic-packages would be the best way to address this.}



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2013-03-29  0:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-29  0:32 Pure vs. Pure; aspect vs. pragma Shark8

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