comp.lang.ada
 help / color / mirror / Atom feed
* Generic package with dynamic subprogram name?
@ 2010-06-09 23:07 Marek Janukowicz
  2010-06-09 23:37 ` Jeffrey R. Carter
  2010-06-10  7:33 ` Stephen Leake
  0 siblings, 2 replies; 10+ messages in thread
From: Marek Janukowicz @ 2010-06-09 23:07 UTC (permalink / raw)


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@netfront.net ---



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Generic package with dynamic subprogram name?
  2010-06-09 23:07 Generic package with dynamic subprogram name? Marek Janukowicz
@ 2010-06-09 23:37 ` Jeffrey R. Carter
  2010-06-10  6:05   ` Ludovic Brenta
  2010-06-10  7:33 ` Stephen Leake
  1 sibling, 1 reply; 10+ messages in thread
From: Jeffrey R. Carter @ 2010-06-09 23:37 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Generic package with dynamic subprogram name?
  2010-06-09 23:37 ` Jeffrey R. Carter
@ 2010-06-10  6:05   ` Ludovic Brenta
  0 siblings, 0 replies; 10+ messages in thread
From: Ludovic Brenta @ 2010-06-10  6:05 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@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.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Generic package with dynamic subprogram name?
  2010-06-09 23:07 Generic package with dynamic subprogram name? Marek Janukowicz
  2010-06-09 23:37 ` Jeffrey R. Carter
@ 2010-06-10  7:33 ` Stephen Leake
  2010-06-10 10:14   ` Brian Drummond
  1 sibling, 1 reply; 10+ messages in thread
From: Stephen Leake @ 2010-06-10  7:33 UTC (permalink / raw)


Marek Janukowicz <marek@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



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Generic package with dynamic subprogram name?
  2010-06-10  7:33 ` Stephen Leake
@ 2010-06-10 10:14   ` Brian Drummond
  2010-06-10 10:48     ` Yannick Duchêne (Hibou57)
                       ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Brian Drummond @ 2010-06-10 10:14 UTC (permalink / raw)


On Thu, 10 Jun 2010 03:33:11 -0400, Stephen Leake
<stephen_leake@stephe-leake.org> wrote:

>Marek Janukowicz <marek@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



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Generic package with dynamic subprogram name?
  2010-06-10 10:14   ` Brian Drummond
@ 2010-06-10 10:48     ` Yannick Duchêne (Hibou57)
  2010-06-10 12:13     ` sjw
  2010-06-11  9:08     ` Stephen Leake
  2 siblings, 0 replies; 10+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2010-06-10 10:48 UTC (permalink / raw)


Le Thu, 10 Jun 2010 12:14:18 +0200, Brian Drummond  
<brian_drummond@btconnect.com> a écrit:

> On Thu, 10 Jun 2010 03:33:11 -0400, Stephen Leake
> <stephen_leake@stephe-leake.org> wrote:
>
>> Marek Janukowicz <marek@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
Not a tutorial, but the only one meaningful source about ASIS I know is
http://www.sigada.org/wg/asiswg/
It contains documentations and some examples, some rather complete.
For examples, look at
http://www.sigada.org/wg/asiswg/ASISWG_Results.html
You may want to search for the two links named “Publically available ASIS  
Applications” and “How to do things in ASIS” in the latter page. Good  
point to start, beside the specification files of your compiler's ASIS  
library.

-- 
There is even better than a pragma Assert: a SPARK --# check.
--# check C and WhoKnowWhat and YouKnowWho;
--# assert Ada;
--  i.e. forget about previous premises which leads to conclusion
--  and start with new conclusion as premise.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Generic package with dynamic subprogram name?
  2010-06-10 10:14   ` Brian Drummond
  2010-06-10 10:48     ` Yannick Duchêne (Hibou57)
@ 2010-06-10 12:13     ` sjw
  2010-06-11  9:08     ` Stephen Leake
  2 siblings, 0 replies; 10+ messages in thread
From: sjw @ 2010-06-10 12:13 UTC (permalink / raw)


On Jun 10, 11:14 am, Brian Drummond <brian_drumm...@btconnect.com>
wrote:
> On Thu, 10 Jun 2010 03:33:11 -0400, Stephen Leake
>
> <stephen_le...@stephe-leake.org> wrote:
> >Marek Janukowicz <ma...@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.

http://www.mckae.com/avatox.html -- "Avatox (Ada, Via Asis, To Xml) is
an application that traverses one or more Ada compilation units and
outputs the ASIS representation of the unit(s) as an XML document. The
resulting XML document(s) can then have an XSL stylesheet applied in
turn." And you can use an XSL stylesheet to generate code.

Another application in the same space is my ASIS2XML --
http://gnat-asis.sourceforge.net/pmwiki.php/Main/ASIS2XML .



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Generic package with dynamic subprogram name?
  2010-06-10 10:14   ` Brian Drummond
  2010-06-10 10:48     ` Yannick Duchêne (Hibou57)
  2010-06-10 12:13     ` sjw
@ 2010-06-11  9:08     ` Stephen Leake
  2010-06-11  9:55       ` J-P. Rosen
  2010-06-11 12:19       ` Brian Drummond
  2 siblings, 2 replies; 10+ messages in thread
From: Stephen Leake @ 2010-06-11  9:08 UTC (permalink / raw)


Brian Drummond <brian_drummond@btconnect.com> writes:

> On Thu, 10 Jun 2010 03:33:11 -0400, Stephen Leake
> <stephen_leake@stephe-leake.org> wrote:
>
>>Marek Janukowicz <marek@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.

I'm not aware of any tutorials.

gnatstub (included in all GNAT distributions) is a small ASIS
application that can serve as a good starting point.

auto_text_io (which I wrote)
(http://www.stephe-leake.org/ada/auto_text_io.html) is a larger example.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Generic package with dynamic subprogram name?
  2010-06-11  9:08     ` Stephen Leake
@ 2010-06-11  9:55       ` J-P. Rosen
  2010-06-11 12:19       ` Brian Drummond
  1 sibling, 0 replies; 10+ messages in thread
From: J-P. Rosen @ 2010-06-11  9:55 UTC (permalink / raw)


Stephen Leake a �crit :
>> If there is a tutorial somewhere using ASIS to generate co... <cough> source
>> program text, I'd be interested to see it.
> 
> I'm not aware of any tutorials.
> 
> gnatstub (included in all GNAT distributions) is a small ASIS
> application that can serve as a good starting point.
> 
> auto_text_io (which I wrote)
> (http://www.stephe-leake.org/ada/auto_text_io.html) is a larger example.

adasubst (available from Adalog's site) can also be seen as such a
program, since it produces a modified version of its input source.

-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Generic package with dynamic subprogram name?
  2010-06-11  9:08     ` Stephen Leake
  2010-06-11  9:55       ` J-P. Rosen
@ 2010-06-11 12:19       ` Brian Drummond
  1 sibling, 0 replies; 10+ messages in thread
From: Brian Drummond @ 2010-06-11 12:19 UTC (permalink / raw)


On Fri, 11 Jun 2010 05:08:55 -0400, Stephen Leake
<stephen_leake@stephe-leake.org> wrote:

>Brian Drummond <brian_drummond@btconnect.com> writes:
>
>> On Thu, 10 Jun 2010 03:33:11 -0400, Stephen Leake
>> <stephen_leake@stephe-leake.org> wrote:

>>>Using an ASIS application to generate code would be another way. 
>>
>> Any pointers (uh, references) where to learn ASIS for this purpose?

>I'm not aware of any tutorials.
>
>gnatstub (included in all GNAT distributions) is a small ASIS
>application that can serve as a good starting point.
>
>auto_text_io (which I wrote)
>(http://www.stephe-leake.org/ada/auto_text_io.html) is a larger example.

Thanks to all for these suggestions, especially yourself, and JP Rosen for
adasubst. Avatox looks interesting but I'm not learning XML just for this!

So auto_text_io looks like the best starting point for me.

I have no definite purpose in mind, but some interest in the possibility of
transforming simple Ada mathematical code into behavioral VHDL.

Mostly because I am appalled at the current trends toward using C to design
hardware! 

- Brian



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-06-11 12:19 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-09 23:07 Generic package with dynamic subprogram name? Marek Janukowicz
2010-06-09 23:37 ` Jeffrey R. Carter
2010-06-10  6:05   ` Ludovic Brenta
2010-06-10  7:33 ` Stephen Leake
2010-06-10 10:14   ` Brian Drummond
2010-06-10 10:48     ` Yannick Duchêne (Hibou57)
2010-06-10 12:13     ` sjw
2010-06-11  9:08     ` Stephen Leake
2010-06-11  9:55       ` J-P. Rosen
2010-06-11 12:19       ` Brian Drummond

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