comp.lang.ada
 help / color / mirror / Atom feed
* Package instance???
@ 2002-02-13 19:48 Yates
  2002-02-13 22:25 ` Zach Swanson
                   ` (6 more replies)
  0 siblings, 7 replies; 12+ messages in thread
From: Yates @ 2002-02-13 19:48 UTC (permalink / raw)


I am new to Ada. I'd like to know if it is possible to create multiple
instances of a package. For example, if I have a package 'My_Package',  can
I do something like:

p1 : My_Package := new My_Package
p2 : My_Package := new My_Package
.....

I tried this and didn't work. Is there other ways to do it?

Thanks

Yates





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

* Re: Package instance???
  2002-02-13 19:48 Package instance??? Yates
@ 2002-02-13 22:25 ` Zach Swanson
  2002-02-13 22:31 ` Jeffrey Carter
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Zach Swanson @ 2002-02-13 22:25 UTC (permalink / raw)


"Yates" <yates_1900@yahoo.com> wrote
> if I have a package 'My_Package',  can
> I do something like:
> 
> p1 : My_Package := new My_Package
> p2 : My_Package := new My_Package

Lets assume you have some packge like ada.integer_text_io.

then => package int_io renames ada.integer_text_io;
will rename that package to your specified identifier. The exact
syntax for package renaming declarations is in section 8.5.3 of the
Language Reference Manual, page 154.

Or you can create an entirely new instance of a package to instantiate
some portion of a generic package.

ie.  => type small_natural is new natural range 1..10;
        package small_natural_io is new   
        ada.text_io.integer_io(small_natural'range);

What you were trying to do in your original example is syntactically
incorrect. If your goal was to create two seperate packages using the
same unit as a base for these, then you should probably look into
generic units.



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

* Re: Package instance???
  2002-02-13 19:48 Package instance??? Yates
  2002-02-13 22:25 ` Zach Swanson
@ 2002-02-13 22:31 ` Jeffrey Carter
  2002-02-14  2:28   ` Zach Swanson
  2002-02-14  2:04 ` Bobby D. Bryant
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 12+ messages in thread
From: Jeffrey Carter @ 2002-02-13 22:31 UTC (permalink / raw)


Yates wrote:
> 
> I am new to Ada. I'd like to know if it is possible to create multiple
> instances of a package. For example, if I have a package 'My_Package',  can
> I do something like:
> 
> p1 : My_Package := new My_Package
> p2 : My_Package := new My_Package
> .....
> 
> I tried this and didn't work. Is there other ways to do it?

You can if My_Package is a generic package. However ...

Packages are a basic concept in Ada, and this question shows such a
level of misunderstanding of packages that it seems unlikely any answer
will be meaningful to you until you gain a better grasp of the language.

You might want to look at www.adapower.com, where you'll find language
tutorials and lots of examples. If you still have a question after
working through a tutorial and reviewing some examples, then we will
probably be able to help you.

-- 
Jeffrey Carter



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

* Re: Package instance???
  2002-02-13 19:48 Package instance??? Yates
  2002-02-13 22:25 ` Zach Swanson
  2002-02-13 22:31 ` Jeffrey Carter
@ 2002-02-14  2:04 ` Bobby D. Bryant
  2002-02-14  2:54 ` Steve Doiel
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Bobby D. Bryant @ 2002-02-14  2:04 UTC (permalink / raw)


On Wed, 13 Feb 2002 13:48:56 -0600, Yates wrote:

> I am new to Ada. I'd like to know if it is possible to create multiple
> instances of a package. For example, if I have a package 'My_Package',
>  can I do something like:
> 
> p1 : My_Package := new My_Package
> p2 : My_Package := new My_Package
> .....
> 
> I tried this and didn't work. Is there other ways to do it?


It would help if you would tell us what you are trying to do.

Are you trying to create multiple instances of some "OO" object?


Bobby Bryant
Austin, Texas



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

* Re: Package instance???
  2002-02-13 22:31 ` Jeffrey Carter
@ 2002-02-14  2:28   ` Zach Swanson
  2002-02-14 14:32     ` Marin David Condic
  0 siblings, 1 reply; 12+ messages in thread
From: Zach Swanson @ 2002-02-14  2:28 UTC (permalink / raw)


Jeffrey Carter <jeffrey.carter@boeing.com> wrote in message 
> > p1 : My_Package := new My_Package
> > p2 : My_Package := new My_Package
> > .....
> > 
> > I tried this and didn't work. Is there other ways to do it?
> 
> You can if My_Package is a generic package. However ...

Nope that fails even if its a generic because the syntax is incorrect.
The declaration should be
p1 : my_package is new some_other_package 
to be correct.



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

* Re: Package instance???
  2002-02-13 19:48 Package instance??? Yates
                   ` (2 preceding siblings ...)
  2002-02-14  2:04 ` Bobby D. Bryant
@ 2002-02-14  2:54 ` Steve Doiel
  2002-02-14 14:41   ` Marin David Condic
  2002-02-14 12:48 ` Marc A. Criley
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 12+ messages in thread
From: Steve Doiel @ 2002-02-14  2:54 UTC (permalink / raw)


"Yates" <yates_1900@yahoo.com> wrote in message
news:OvBYEeMtBHA.1400@cpimsnntpa03...
> I am new to Ada. I'd like to know if it is possible to create multiple
> instances of a package. For example, if I have a package 'My_Package',
can
> I do something like:
>
> p1 : My_Package := new My_Package
> p2 : My_Package := new My_Package
> .....
>
> I tried this and didn't work. Is there other ways to do it?
>

Just to round out the discussion, yes you can do something kind of like
that, but that's probably not what your really want....

If your current package is defined as:

package My_Package is
... your package spec declarations here
end My_Package;

package body My_Package is
... your package implementation code here
end My_Package;

You can change your package spec to:
generic
package My_Package is
... your package spec declarations here
end My_Package;

And then you can define:

  p1 is new My_Package;
  p2 is new My_Package;

Which will create two instances of a My_Package.
I don't know your background, but if you have C++ for comparison a generic
in Ada is kind of like a template in C++, not an object.

I hope this helps,
SteveD

> Thanks
>
> Yates
>
>





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

* Re: Package instance???
  2002-02-13 19:48 Package instance??? Yates
                   ` (3 preceding siblings ...)
  2002-02-14  2:54 ` Steve Doiel
@ 2002-02-14 12:48 ` Marc A. Criley
  2002-02-14 15:09 ` Yates
  2002-02-14 18:12 ` Matthew Heaney
  6 siblings, 0 replies; 12+ messages in thread
From: Marc A. Criley @ 2002-02-14 12:48 UTC (permalink / raw)


Yates wrote:
> 
> I am new to Ada. I'd like to know if it is possible to create multiple
> instances of a package. For example, if I have a package 'My_Package',  can
> I do something like:
> 
> p1 : My_Package := new My_Package
> p2 : My_Package := new My_Package
> .....
> 
> I tried this and didn't work. Is there other ways to do it?

I think you're perhaps thinking of a package as analogous to a C++
class.  While there are some surface similarities, packages and classes
embody different concepts.

Disregarding the other postings about generic packages, a package is an
encapsulation mechanism, i.e., a way to group data and functionality. 
The approximate equivalent of a C++ class is an Ada tagged type and its
primitive operations (a single set of which are normally grouped
together in a package).  If you wanted multiple instances of such a
"class", you would have multiple variable declarations of that tagged
type--or dynamically allocate them.  Packages serve to prevent making
all type (class) names globally visible, a capability that C++
namespaces was introduced to provide.

Because packages are not classes, there's more freedom about what they
can contain--constants, type definitions, supporting type definitions,
subprograms that are not directly associated with the package's tagged
type (but provide some functionality in support of it)--in short,
everything that's needed to define a class.  In this sense, a package is
more like a C++ file, which will contain the class definition as well as
the definitions and functions that aid the definition of the contained
class.  But the Ada package provides a stronger encapsulation of those
definitions than does a file.

Marc A. Criley
Consultant
Quadrus Corporation
www.quadruscorp.com



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

* Re: Package instance???
  2002-02-14  2:28   ` Zach Swanson
@ 2002-02-14 14:32     ` Marin David Condic
  0 siblings, 0 replies; 12+ messages in thread
From: Marin David Condic @ 2002-02-14 14:32 UTC (permalink / raw)


"Zach Swanson" <x24702@usma.edu> wrote in message
news:50c1a1b.0202131828.61cc30db@posting.google.com...
>
> Nope that fails even if its a generic because the syntax is incorrect.
> The declaration should be
> p1 : my_package is new some_other_package
> to be correct.

Maybe you meant "package My_Package is new Some_Other_Package ;"

From ARM 12.3 (2):

generic_instantiation ::=
     package defining_program_unit_name is
       new generic_package_name [generic_actual_part];
   | procedure defining_program_unit_name is
       new generic_procedure_name [generic_actual_part];
   | function defining_designator is
       new generic_function_name [generic_actual_part];



MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/






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

* Re: Package instance???
  2002-02-14  2:54 ` Steve Doiel
@ 2002-02-14 14:41   ` Marin David Condic
  2002-02-15  2:13     ` Steve Doiel
  0 siblings, 1 reply; 12+ messages in thread
From: Marin David Condic @ 2002-02-14 14:41 UTC (permalink / raw)



"Steve Doiel" <nospam_steved94@attbi.com> wrote in message
news:fFFa8.54055$AV5.312647@rwcrnsc51.ops.asp.att.net...
>
> And then you can define:
>
>   p1 is new My_Package;
>   p2 is new My_Package;
>
Syntax check: Should be "package p1 is new My_Package;" - but see below.


> Which will create two instances of a My_Package.
> I don't know your background, but if you have C++ for comparison a generic
> in Ada is kind of like a template in C++, not an object.
>
I strongly suspect from the way the question was stated that Mr. Yates
(first or last name?) was trying to duplicate what can be done with a C++
class - that is, have a bunch of variables bundled together with some
operations and make several instances of the set of variables. While, yes,
you *can* get a similar effect using generics - that doesn't seem to be what
is being called for. I'd suggest that generics best be left out of it for
the time being because its a little tricky for a newbie.

It would help if we got a little better description of what the desired
behavior is so we can suggest the proper Ada idiom for getting there.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/





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

* Re: Package instance???
  2002-02-13 19:48 Package instance??? Yates
                   ` (4 preceding siblings ...)
  2002-02-14 12:48 ` Marc A. Criley
@ 2002-02-14 15:09 ` Yates
  2002-02-14 18:12 ` Matthew Heaney
  6 siblings, 0 replies; 12+ messages in thread
From: Yates @ 2002-02-14 15:09 UTC (permalink / raw)


Thanks to all of you.

Yates

"Yates" <yates_1900@yahoo.com> wrote in message
news:OvBYEeMtBHA.1400@cpimsnntpa03...
> I am new to Ada. I'd like to know if it is possible to create multiple
> instances of a package. For example, if I have a package 'My_Package',
can
> I do something like:
>
> p1 : My_Package := new My_Package
> p2 : My_Package := new My_Package
> .....
>
> I tried this and didn't work. Is there other ways to do it?
>
> Thanks
>
> Yates
>
>





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

* Re: Package instance???
  2002-02-13 19:48 Package instance??? Yates
                   ` (5 preceding siblings ...)
  2002-02-14 15:09 ` Yates
@ 2002-02-14 18:12 ` Matthew Heaney
  6 siblings, 0 replies; 12+ messages in thread
From: Matthew Heaney @ 2002-02-14 18:12 UTC (permalink / raw)



"Yates" <yates_1900@yahoo.com> wrote in message
news:OvBYEeMtBHA.1400@cpimsnntpa03...
> I am new to Ada. I'd like to know if it is possible to create multiple
> instances of a package. For example, if I have a package 'My_Package',
can
> I do something like:
>
> p1 : My_Package := new My_Package
> p2 : My_Package := new My_Package
> .....
>
> I tried this and didn't work. Is there other ways to do it?

A package is not a type, so you can't do this.

You could declare My_Package as a generic, and then make multiple
instantiations:

generic
package GP is ...;

package P1 is new GP;
package P2 is new GP;

which is close to your original example.

Of course, unless you have static data in the package, I am skeptical of
your need to do this.  Why isn't declaring an instance of a type adequate?
For example:

package P is
   type T is ...;
end;

O1 : P.T;
O2 : P.T;







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

* Re: Package instance???
  2002-02-14 14:41   ` Marin David Condic
@ 2002-02-15  2:13     ` Steve Doiel
  0 siblings, 0 replies; 12+ messages in thread
From: Steve Doiel @ 2002-02-15  2:13 UTC (permalink / raw)



"Marin David Condic" <dont.bother.mcondic.auntie.spam@[acm.org> wrote in
message news:a4gib7$fr6$1@nh.pace.co.uk...
>
> "Steve Doiel" <nospam_steved94@attbi.com> wrote in message
> news:fFFa8.54055$AV5.312647@rwcrnsc51.ops.asp.att.net...
> >
> > And then you can define:
> >
> >   p1 is new My_Package;
> >   p2 is new My_Package;
> >
> Syntax check: Should be "package p1 is new My_Package;" - but see below.
>
>
That's what I get for not compiling my example (which I usually do).

Shame on me ;-)

SteveD






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

end of thread, other threads:[~2002-02-15  2:13 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-13 19:48 Package instance??? Yates
2002-02-13 22:25 ` Zach Swanson
2002-02-13 22:31 ` Jeffrey Carter
2002-02-14  2:28   ` Zach Swanson
2002-02-14 14:32     ` Marin David Condic
2002-02-14  2:04 ` Bobby D. Bryant
2002-02-14  2:54 ` Steve Doiel
2002-02-14 14:41   ` Marin David Condic
2002-02-15  2:13     ` Steve Doiel
2002-02-14 12:48 ` Marc A. Criley
2002-02-14 15:09 ` Yates
2002-02-14 18:12 ` Matthew Heaney

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