From: Marek Janukowicz on
Hello

(Disclaimer: the code won't compile and some parameters are omitted, but
everyone should get the idea).

I need a number of function/procedure pairs and generics seem to be the way
to go.

I can use something like this:
generic
Attr : String;
function Get_Attribute return String;
generic
Attr : String;
procedure Set_Attribute ( Value : String );

and instantiate:
function Get_First_Name is new Get_Attribute( "FirstName" );
procedure Set_First_Name is new Set_Attribute( "FirstName" );
function Get_Last_Name is new Get_Attribute( "LastName" );
procedure Set_Last_Name is new Set_Attribute( "LastName" );

However, I don't like passing the same parameters to instantiation of both
the function and the procedure (as mentioned earlier there are more
parameters and they are always the same for Get/Set instantiations). So I
came up with something like this:

generic
Attr : String;
package Get_Set is
function Get_Attribute return String;
procedure Set_Attribute ( Value : String );
end Get_Set;

But if I then instantiate:
package Get_Set_First_Name is new Get_Set( "FirstName" );
package Get_Set_Last_Name is new Get_Set( "LastName" );

subprogram names will overlap. I know I can call them using package prefix,
but I'd really like to have nice API like here:
Get_First_Name( ... );
Set_Last_Name( ... );

I understand things I ask about in the subject are most likely not possible,
but is there any other way to achieve what I want? I will also appreciate
suggestions of different approaches, as I didn't program in Ada for a few
years now and I can't say I know the language well.

Thanks
--
Marek Janukowicz

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Jeffrey R. Carter on
Marek Janukowicz wrote:
>
> generic
> Attr : String;
> package Get_Set is
> function Get_Attribute return String;
> procedure Set_Attribute ( Value : String );
> end Get_Set;
>
> But if I then instantiate:
> package Get_Set_First_Name is new Get_Set( "FirstName" );
> package Get_Set_Last_Name is new Get_Set( "LastName" );
>
> subprogram names will overlap. I know I can call them using package prefix,
> but I'd really like to have nice API like here:
> Get_First_Name( ... );
> Set_Last_Name( ... );

function Get_First_Name return String renames Get_Set_First_Name.Get_Attribute;
procedure Set_First_Name (First_Name : in String) renames
Get_Set_First_Name.Set_Attribute;

--
Jeff Carter
"I've got to stay here, but there's no reason
why you folks shouldn't go out into the lobby
until this thing blows over."
Horse Feathers
50
From: Ludovic Brenta on
"Jeffrey R. Carter" <spam.jrcarter.not(a)spam.acm.org> writes:

> Marek Janukowicz wrote:
>>
>> generic
>> Attr : String;
>> package Get_Set is
>> function Get_Attribute return String;
>> procedure Set_Attribute ( Value : String );
>> end Get_Set;
>>
>> But if I then instantiate:
>> package Get_Set_First_Name is new Get_Set( "FirstName" );
>> package Get_Set_Last_Name is new Get_Set( "LastName" );
>>
>> subprogram names will overlap. I know I can call them using package
>> prefix, but I'd really like to have nice API like here:
>> Get_First_Name( ... );
>> Set_Last_Name( ... );
>
> function Get_First_Name return String renames Get_Set_First_Name.Get_Attribute;
> procedure Set_First_Name (First_Name : in String) renames
> Get_Set_First_Name.Set_Attribute;

generic
Name : String;
package Attribute is
function Get return String;
procedure Set ( Value : String );
end Attribute;

package First_Name is new Attribute (Name => "FirstName");
package Last_Name is new Attribute (Name => "LastName");

First_Name_Attribute := First_Name.Get;
Last_Name.Set (...);

--
Ludovic Brenta.
From: Stephen Leake on
Marek Janukowicz <marek(a)janukowicz.net> writes:

> generic
> Attr : String;
> package Get_Set is
> function Get_Attribute return String;
> procedure Set_Attribute ( Value : String );
> end Get_Set;
>
> But if I then instantiate:
> package Get_Set_First_Name is new Get_Set( "FirstName" );
> package Get_Set_Last_Name is new Get_Set( "LastName" );
>
> subprogram names will overlap. I know I can call them using package prefix,
> but I'd really like to have nice API like here:
> Get_First_Name( ... );
> Set_Last_Name( ... );

Use better names for the generic, so the package prefix is not just noise:

generic
Attribute : String;
package Attributes is
function Get return String;
procedure Set ( Value : String );
end Get_Set;

package First_Name is new Attributes( "FirstName" );
package Last_Name is new Attributes( "LastName" );

First_Name.Get( ... );
Last_Name.Set( ... );

> I understand things I ask about in the subject are most likely not possible,
> but is there any other way to achieve what I want?

Renames was suggested. Writing all of those can get tedious, and
probably defeats the purpose of the generic.

Using an ASIS application to generate code would be another way.

--
-- Stephe
From: Brian Drummond on
On Thu, 10 Jun 2010 03:33:11 -0400, Stephen Leake
<stephen_leake(a)stephe-leake.org> wrote:

>Marek Janukowicz <marek(a)janukowicz.net> writes:

>> I understand things I ask about in the subject are most likely not possible,
>> but is there any other way to achieve what I want?
>
>Renames was suggested. Writing all of those can get tedious, and
>probably defeats the purpose of the generic.
>
>Using an ASIS application to generate code would be another way.

Any pointers (uh, references) where to learn ASIS for this purpose?
I've always understood ASIS as being used to analyze Ada, not generate it...

If there is a tutorial somewhere using ASIS to generate co... <cough> source
program text, I'd be interested to see it.

- Brian