How to extend packages

This is a discussion on How to extend packages within the ADA forums in Programming Languages category; Hi, Ada-folks! I'd like to extend the GNAT.Regpat a little bit, so that it contains additional operations like Split (the same as in Perl or Ruby) or Join (dito). When speaking of "extending" I mean it in the sense of e.g. Java where extending a class means that the child class contains all the public or protected operations and attributes as the super class. Perhaps you know now, what I want to achieve. I want to have an extended "child" package of GNAT.Regpat. In the end it should have an own name like e.g. "Mine.Regex" and contain all operation etc. ...

Go Back   Application Development Forum > Programming Languages > ADA

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 06-06-2008, 01:00 PM
snoopysalive@googlemail.com
Guest
 
Default How to extend packages

Hi, Ada-folks!

I'd like to extend the GNAT.Regpat a little bit, so that it contains
additional operations like Split (the same as in Perl or Ruby) or Join
(dito).

When speaking of "extending" I mean it in the sense of e.g. Java where
extending a class means that the child class contains all the public
or protected operations and attributes as the super class. Perhaps you
know now, what I want to achieve. I want to have an extended "child"
package of GNAT.Regpat. In the end it should have an own name like
e.g. "Mine.Regex" and contain all operation etc. of its "super"
package plus the operations I'll additionally implement.

I've been trying for hours now, so I'm passing this question to you.
How to extend package?

Thanks,
Matthias
Reply With Quote
  #2  
Old 06-06-2008, 01:24 PM
witmer
Guest
 
Default Re: How to extend packages

On Jun 6, 1:00 pm, "snoopysal...@googlemail.com"
<snoopysal...@googlemail.com> wrote:
> Hi, Ada-folks!
>
> I'd like to extend the GNAT.Regpat a little bit, so that it contains
> additional operations like Split (the same as in Perl or Ruby) or Join
> (dito).
>
> When speaking of "extending" I mean it in the sense of e.g. Java where
> extending a class means that the child class contains all the public
> or protected operations and attributes as the super class. Perhaps you
> know now, what I want to achieve. I want to have an extended "child"
> package of GNAT.Regpat. In the end it should have an own name like
> e.g. "Mine.Regex" and contain all operation etc. of its "super"
> package plus the operations I'll additionally implement.
>
> I've been trying for hours now, so I'm passing this question to you.
> How to extend package?
>
> Thanks,
> Matthias


I don't there's a way to directly do what you want, but if I'm
understanding you correctly, this should do it:

--"Parent" spec
package Parent is
procedure Proc_1;
procedure Proc_2;
--etc...
end Parent;

--"Child" spec
with Parent;

package Child is
procedure Proc_1 renames Parent.Proc1;
procedure Proc_2 renames Parent.Proc2;
--and so on...

--Child members go here
end Child;
Reply With Quote
  #3  
Old 06-06-2008, 01:45 PM
Sébastien Morand
Guest
 
Default Re: How to extend packages

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

snoopysalive@googlemail.com a écrit :
> Hi, Ada-folks!
>
> I'd like to extend the GNAT.Regpat a little bit, so that it contains
> additional operations like Split (the same as in Perl or Ruby) or Join
> (dito).
>
> When speaking of "extending" I mean it in the sense of e.g. Java where
> extending a class means that the child class contains all the public
> or protected operations and attributes as the super class. Perhaps you
> know now, what I want to achieve. I want to have an extended "child"
> package of GNAT.Regpat. In the end it should have an own name like
> e.g. "Mine.Regex" and contain all operation etc. of its "super"
> package plus the operations I'll additionally implement.
>
> I've been trying for hours now, so I'm passing this question to you.
> How to extend package?


Try this:

with GNAT.Regpat; use GNAT.Regpat;

package GNAT.Regpat.Extended is
end GNAT.Regpat.Extended;

Then when you use GNAT.Regpat.Extended, GNAT.Regpat is available too.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)

iD8DBQFISXer+zV9xm4PlDQRAg3JAJ9C0PKClnt6uc1Tjl85b1 zvZfQrmgCfcQgm
1tLnIgHiSGjifzvMyBvwOgc=
=JX0R
-----END PGP SIGNATURE-----
Reply With Quote
  #4  
Old 06-06-2008, 02:16 PM
Georg Bauhaus
Guest
 
Default Re: How to extend packages

snoopysalive@googlemail.com wrote:
> Hi, Ada-folks!
>
> I'd like to extend the GNAT.Regpat a little bit, so that it contains
> additional operations like Split (the same as in Perl or Ruby) or Join
> (dito).


A Split subprogram is available in the AWK section of the GNAT library.

> When speaking of "extending" I mean it in the sense of e.g. Java where
> extending a class means that the child class contains all the public
> or protected operations and attributes as the super class.


You can't extend packages in Java either ;-) Like you have said,
you extend a class and so in Ada, you extend a type T by deriving
a new type D from T. You can do that in the same package, or in a nested
package, or in a child package, or in some other package.
Since GNAT.Regpat should not normally be touched (it lives in compiler
file space), you would indeed write a child package in this case;
but note that a child _package_ is a child in the sence of a
hierarchical library of modules, like "util" would be a
"child package" of "java" in "java.util" if you did use Ada terms
for describing Java packages.



> Perhaps you
> know now, what I want to achieve. I want to have an extended "child"
> package of GNAT.Regpat. In the end it should have an own name like
> e.g. "Mine.Regex" and contain all operation etc. of its "super"
> package plus the operations I'll additionally implement.


Just in case you only need more RE matching capabilities,
be sure to look an GNAT.Spitbol.Patterns. SPITBOL patterns have
everything that Perl5 REs have, including a working \G.

> I've been trying for hours now, so I'm passing this question to you.
> How to extend package?


You don't.



-- Georg
Reply With Quote
  #5  
Old 06-07-2008, 10:21 AM
Stephen Leake
Guest
 
Default Re: How to extend packages

Sébastien Morand <seb.morand@gmail.com> writes:

> snoopysalive@googlemail.com a écrit :
>> Hi, Ada-folks!
>>
>> I'd like to extend the GNAT.Regpat a little bit, so that it contains
>> additional operations like Split (the same as in Perl or Ruby) or Join
>> (dito).
>>
>> When speaking of "extending" I mean it in the sense of e.g. Java where
>> extending a class means that the child class contains all the public
>> or protected operations and attributes as the super class. Perhaps you
>> know now, what I want to achieve. I want to have an extended "child"
>> package of GNAT.Regpat. In the end it should have an own name like
>> e.g. "Mine.Regex" and contain all operation etc. of its "super"
>> package plus the operations I'll additionally implement.
>>
>> I've been trying for hours now, so I'm passing this question to you.
>> How to extend package?

>
> Try this:
>
> with GNAT.Regpat; use GNAT.Regpat;
>
> package GNAT.Regpat.Extended is
> end GNAT.Regpat.Extended;


This is probably the best approach.

> Then when you use GNAT.Regpat.Extended, GNAT.Regpat is available too.


This is not true. To be precise:

Suppose GNAT.Regpat declares 'foo', and GNAT.Regpat.Extended declares
'bar'.

with GNAT.Regpat.Extended;
package A is
....
end A;

Within A, you can say GNAT.Regpat.Extended.foo, or GNAT.Regpat.bar,
but 'foo' and 'bar' by themselves are not visible.

with GNAT.Regpat.Extended;
package B is
use GNAT.Regpat.Extended;
....
end B;

Within B, you can say GNAT.Regpat.Extended.foo, or GNAT.Regpat.bar.
'foo' is visible by itself, but 'bar' is not.

with GNAT.Regpat.Extended;
package C is
use GNAT.Regpat.Extended;
use GNAT.Regpat;
....
end C;

Within C, 'foo' and 'bar' are both visible.

--
-- Stephe
Reply With Quote
  #6  
Old 06-07-2008, 12:04 PM
Simon Wright
Guest
 
Default Re: How to extend packages

Sébastien Morand <seb.morand@gmail.com> writes:

> with GNAT.Regpat; use GNAT.Regpat;


This is not necessary; the parent's spec (including the private part)
is directly visible within the child.

Though I'm not sure what help that would be in future, since in 4.3.0
the spec is ..

with System.Regpat;

package GNAT.Regpat renames System.Regpat;

> package GNAT.Regpat.Extended is
> end GNAT.Regpat.Extended;


But the only private type in Regpat is Pattern_Matcher. Is knowledge
of Pattern_Matcher required in order to write OP's extensions? If so,
OP will have to write a child (of System.Regpat, I think).
Reply With Quote
  #7  
Old 06-07-2008, 05:50 PM
snoopysalive@googlemail.com
Guest
 
Default Re: How to extend packages

Ok, I've tried out Sébastien Morand's solution but it's still not
working. To be honest, it's possible that I'm just using the compiler
in a wrong way.

Here's my code:

------------------
-- s-regext.ads --
------------------

with System.Regpat;

package System.Regpat.Extended is
end System.Regpat.Extended;

------------------
-- s-regext.adb --
------------------

pragma No_Body;

-------------
-- rex.adb --
-------------

with System.Regpat.Extended;

procedure Rex is
begin
null;
end Rex;



When compiling I get this messages:

$ gnatmake rex.adb
gnatbind -x rex.ali
error: "s-regext.ali" not found, "s-regext.ads" must be compiled
gnatmake: *** bind failed.


So, I try to comile s-regext.ads first and get this message:

$ gnatmake s-regext.ads
gnatmake: not allowed to compile "s-regext.ads"; use -a switch, or
compile file with "-gnatg" switch


When trying the -a switch:

$ gnatmake -a s-regext.ads
gcc -gnatpg -c s-regext.ads
s-regext.ads:1:12: warning: no entities of "Regpat" are referenced
gnatmake: "s-regext.ads" compilation error


When trying the -gnatg switch:

$ gnatmake -gnatg s-regext.ads
gnatmake: not allowed to compile "s-regext.ads"; use -a switch, or
compile file with "-gnatg" switch


I don't really understand what's happening here because when
simulating this kind of "inheritance" with packages written by myself,
it is working. Here's the example code:

--------------
-- test.ads --
--------------

with Ada.Text_IO;

package Test is

procedure Say_Hello;

end Test;


--------------
-- test.adb --
--------------

package body Test is

procedure Say_Hello is
begin
Ada.Text_IO.Put_Line ("Say_Hello");
end Say_Hello;

end Test;


------------------
-- test-run.ads --
------------------

with Test;

package Test.Run is

procedure Say_Something;

end Test.Run;


------------------
-- test-run.adb --
------------------

package body Test.Run is
use Test;

procedure Say_Something is
begin
Ada.Text_IO.Put_Line ("Say_Something");
end Say_Something;

end Test.Run;


--------------
-- main.adb --
--------------

with Test.Run;

procedure Main is
begin
Test.Say_Hello;
Test.Run.Say_Something;
end Main;



$ gnatmake main.adb
gcc -c main.adb
gnatbind -x main.ali
gnatlink main.ali

$ ./main
Say_Hello
Say_Something


So, it seems as if it's not possible to extend packages of the
standard library. Or am I interpreting this in a wrong way?

Last but not least: Thanks for all your answers to my question.

Thanks,
Matthias
Reply With Quote
  #8  
Old 06-08-2008, 07:09 AM
Simon Wright
Guest
 
Default Re: How to extend packages

Nearly there, I think.

You must use the -a switch, because your new code is an extension to
the compiler library. But one of the GNAT rules for compiling the
compiler library is that warnings are treated as errors. So you have
to fix your code so that it creates no warning messages. One way of
doing this is by using pragma Warnings (Off)! but this should only be
temporary or very very local.
Reply With Quote
  #9  
Old 06-09-2008, 06:38 PM
snoopysalive@googlemail.com
Guest
 
Default Re: How to extend packages

Ok, my codes are compiling and working now. Thanks once more. But,
Simon, can you explain me, where to use "pragma Warnings (Off)"
correctly? Thank you.
Reply With Quote
  #10  
Old 06-10-2008, 01:40 AM
Simon Wright
Guest
 
Default Re: How to extend packages

"snoopysalive@googlemail.com" <snoopysalive@googlemail.com> writes:

> Ok, my codes are compiling and working now. Thanks once more. But,
> Simon, can you explain me, where to use "pragma Warnings (Off)"
> correctly? Thank you.


This suppresses thw warning that was causing trouble ..

with System.Regpat;
pragma Warnings (Off, System.Regpat);

package System.Regpat.Extended is
end System.Regpat.Extended;
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:11 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.