comp.lang.ada
 help / color / mirror / Atom feed
* using an Ada.Container as a private type
@ 2008-04-03 15:29 Graham
  2008-04-03 16:00 ` Robert A Duff
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Graham @ 2008-04-03 15:29 UTC (permalink / raw)


Hi,
    I'm writing a little financial application. I want to model a
stream of payments made at arbitrary periods (the periods are positive
numbers; they might be actual dates later on).

I think I want to declare this:


type Payment_Stream is private;

procedure Add_To_Payment_Stream(
            stream : in out Payment_Stream;
            period :  Positive;
            amount : Money );

......

and then privately declare the payment stream as an Ordered Map, like:

private

   package Payment_Stream_Package is new Ada.Containers.Ordered_Maps(
      Element_Type => Money,
      Key_Type => Positive );

   type Payment_Stream is new Payment_Stream_Package.Map;

but I can't do this. Gnat complains that "type derived from tagged
type must have extension".
I'm fairly new to Ada but thought I was getting on top of it! What is
the principal that stops me doing this? What should I do instead?

thanks,

Graham Stark



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

* Re: using an Ada.Container as a private type
  2008-04-03 15:29 using an Ada.Container as a private type Graham
@ 2008-04-03 16:00 ` Robert A Duff
  2008-04-03 16:02 ` Dmitry A. Kazakov
  2008-04-03 20:41 ` Maciej Sobczak
  2 siblings, 0 replies; 9+ messages in thread
From: Robert A Duff @ 2008-04-03 16:00 UTC (permalink / raw)


Graham <graham.stark@virtual-worlds.biz> writes:

>    type Payment_Stream is new Payment_Stream_Package.Map;
>
> but I can't do this. Gnat complains that "type derived from tagged
> type must have extension".
> I'm fairly new to Ada but thought I was getting on top of it! What is
> the principal that stops me doing this? What should I do instead?

Not really a "principle" -- more like an arbitrary restriction.
Map is tagged, and tagged types have to obey that rule.

You should say:

    type Payment_Stream is new Payment_Stream_Package.Map
        with null record;

to create an empty extension.

- Bob



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

* Re: using an Ada.Container as a private type
  2008-04-03 15:29 using an Ada.Container as a private type Graham
  2008-04-03 16:00 ` Robert A Duff
@ 2008-04-03 16:02 ` Dmitry A. Kazakov
  2008-04-03 16:20   ` Graham
  2008-04-03 20:41 ` Maciej Sobczak
  2 siblings, 1 reply; 9+ messages in thread
From: Dmitry A. Kazakov @ 2008-04-03 16:02 UTC (permalink / raw)


On Thu, 3 Apr 2008 08:29:28 -0700 (PDT), Graham wrote:

> Hi,
>     I'm writing a little financial application. I want to model a
> stream of payments made at arbitrary periods (the periods are positive
> numbers; they might be actual dates later on).
> 
> I think I want to declare this:
> 
> 
> type Payment_Stream is private;
> 
> procedure Add_To_Payment_Stream(
>             stream : in out Payment_Stream;
>             period :  Positive;
>             amount : Money );
> 
> ......
> 
> and then privately declare the payment stream as an Ordered Map, like:
> 
> private
> 
>    package Payment_Stream_Package is new Ada.Containers.Ordered_Maps(
>       Element_Type => Money,
>       Key_Type => Positive );
> 
>    type Payment_Stream is new Payment_Stream_Package.Map;

   type Payment_Stream is new Payment_Stream_Package.Map with null record;

> but I can't do this. Gnat complains that "type derived from tagged
> type must have extension".

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: using an Ada.Container as a private type
  2008-04-03 16:02 ` Dmitry A. Kazakov
@ 2008-04-03 16:20   ` Graham
  0 siblings, 0 replies; 9+ messages in thread
From: Graham @ 2008-04-03 16:20 UTC (permalink / raw)


On Apr 3, 5:02 pm, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:
> On Thu, 3 Apr 2008 08:29:28 -0700 (PDT), Graham wrote:
> > Hi,
> >     I'm writing a little financial application. I want to model a
> > stream of payments made at arbitrary periods (the periods are positive
> > numbers; they might be actual dates later on).
>
> > I think I want to declare this:
>
> > type Payment_Stream is private;
>
> > procedure Add_To_Payment_Stream(
> >             stream : in out Payment_Stream;
> >             period :  Positive;
> >             amount : Money );
>
> > ......
>
> > and then privately declare the payment stream as an Ordered Map, like:
>
> > private
>
> >    package Payment_Stream_Package is new Ada.Containers.Ordered_Maps(
> >       Element_Type => Money,
> >       Key_Type => Positive );
>
> >    type Payment_Stream is new Payment_Stream_Package.Map;
>
>    type Payment_Stream is new Payment_Stream_Package.Map with null record;
>
> > but I can't do this. Gnat complains that "type derived from tagged
> > type must have extension".
>
> --
> Regards,
> Dmitry A. Kazakovhttp://www.dmitry-kazakov.de

Oh, I see (I think..) Thanks very much to both of you.

Graham



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

* Re: using an Ada.Container as a private type
  2008-04-03 15:29 using an Ada.Container as a private type Graham
  2008-04-03 16:00 ` Robert A Duff
  2008-04-03 16:02 ` Dmitry A. Kazakov
@ 2008-04-03 20:41 ` Maciej Sobczak
  2008-04-04  8:02   ` Graham
  2 siblings, 1 reply; 9+ messages in thread
From: Maciej Sobczak @ 2008-04-03 20:41 UTC (permalink / raw)


On 3 Kwi, 17:29, Graham <graham.st...@virtual-worlds.biz> wrote:

>    package Payment_Stream_Package is new Ada.Containers.Ordered_Maps(
>       Element_Type => Money,
>       Key_Type => Positive );
>
>    type Payment_Stream is new Payment_Stream_Package.Map;

Instead of a solution, I have a question:

Why do you derive from Map? Which operations of Map do you plan to
override?

Do you ever plan to pass Payment_Stream where
Payment_Stream_Package.Map'Class is expected? What effect would you
like to achieved this way?

Why derivation and not composition?

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com



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

* Re: using an Ada.Container as a private type
  2008-04-03 20:41 ` Maciej Sobczak
@ 2008-04-04  8:02   ` Graham
  2008-04-04 15:57     ` Adam Beneschan
  2008-04-04 17:25     ` Robert A Duff
  0 siblings, 2 replies; 9+ messages in thread
From: Graham @ 2008-04-04  8:02 UTC (permalink / raw)


On Apr 3, 9:41 pm, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
> On 3 Kwi, 17:29, Graham <graham.st...@virtual-worlds.biz> wrote:
>
> >    package Payment_Stream_Package is new Ada.Containers.Ordered_Maps(
> >       Element_Type => Money,
> >       Key_Type => Positive );
>
> >    type Payment_Stream is new Payment_Stream_Package.Map;
>
> Instead of a solution, I have a question:
>
> Why do you derive from Map? Which operations of Map do you plan to
> override?
>
> Do you ever plan to pass Payment_Stream where
> Payment_Stream_Package.Map'Class is expected? What effect would you
> like to achieved this way?
>
> Why derivation and not composition?
>
> --
> Maciej Sobczak *www.msobczak.com*www.inspirel.com

Good question. The intention was not to derive from Map at all: I
simply wanted to keep the fact that it was implemented as a Map
private. Is there a better way of doing that, or is keeping it private
overdesign?

Graham



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

* Re: using an Ada.Container as a private type
  2008-04-04  8:02   ` Graham
@ 2008-04-04 15:57     ` Adam Beneschan
  2008-04-04 17:25     ` Robert A Duff
  1 sibling, 0 replies; 9+ messages in thread
From: Adam Beneschan @ 2008-04-04 15:57 UTC (permalink / raw)


On Apr 4, 1:02 am, Graham <graham.st...@virtual-worlds.biz> wrote:
> On Apr 3, 9:41 pm, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
>
>
>
> > On 3 Kwi, 17:29, Graham <graham.st...@virtual-worlds.biz> wrote:
>
> > >    package Payment_Stream_Package is new Ada.Containers.Ordered_Maps(
> > >       Element_Type => Money,
> > >       Key_Type => Positive );
>
> > >    type Payment_Stream is new Payment_Stream_Package.Map;
>
> > Instead of a solution, I have a question:
>
> > Why do you derive from Map? Which operations of Map do you plan to
> > override?
>
> > Do you ever plan to pass Payment_Stream where
> > Payment_Stream_Package.Map'Class is expected? What effect would you
> > like to achieved this way?
>
> > Why derivation and not composition?
>
> > --
> > Maciej Sobczak *www.msobczak.com*www.inspirel.com
>
> Good question. The intention was not to derive from Map at all: I
> simply wanted to keep the fact that it was implemented as a Map
> private. Is there a better way of doing that, or is keeping it private
> overdesign?

No, that's the sort of thing you're supposed to do.  If you had
derived it in the visible (public) part, you're telling all users of
the package that all the operations available on
Payment_Stream_Package.Map are available for a Payment_Stream, and
that's apparently not what you want.  So doing what you did is the
right thing.

I suspect Maciej just missed the fact that the derived type
declaration was in the private part.

                                 -- Adam



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

* Re: using an Ada.Container as a private type
  2008-04-04  8:02   ` Graham
  2008-04-04 15:57     ` Adam Beneschan
@ 2008-04-04 17:25     ` Robert A Duff
  2008-04-04 19:37       ` Simon Wright
  1 sibling, 1 reply; 9+ messages in thread
From: Robert A Duff @ 2008-04-04 17:25 UTC (permalink / raw)


Graham <graham.stark@virtual-worlds.biz> writes:

> Good question. The intention was not to derive from Map at all: I
> simply wanted to keep the fact that it was implemented as a Map
> private. Is there a better way of doing that,...

You could say:

    type Payment_Stream is
        record
            M : Payment_Stream_Package.Map;
        end record;

which might be better, depending on what you want.

>... or is keeping it private
> overdesign?

No.

- Bob



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

* Re: using an Ada.Container as a private type
  2008-04-04 17:25     ` Robert A Duff
@ 2008-04-04 19:37       ` Simon Wright
  0 siblings, 0 replies; 9+ messages in thread
From: Simon Wright @ 2008-04-04 19:37 UTC (permalink / raw)


Robert A Duff <bobduff@shell01.TheWorld.com> writes:

> Graham <graham.stark@virtual-worlds.biz> writes:
>
>> Good question. The intention was not to derive from Map at all: I
>> simply wanted to keep the fact that it was implemented as a Map
>> private. Is there a better way of doing that,...
>
> You could say:
>
>     type Payment_Stream is
>         record
>             M : Payment_Stream_Package.Map;
>         end record;
>
> which might be better, depending on what you want.

You could also, for example, include a mutex in the record for locking
in the presence of concurrency.



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

end of thread, other threads:[~2008-04-04 19:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-03 15:29 using an Ada.Container as a private type Graham
2008-04-03 16:00 ` Robert A Duff
2008-04-03 16:02 ` Dmitry A. Kazakov
2008-04-03 16:20   ` Graham
2008-04-03 20:41 ` Maciej Sobczak
2008-04-04  8:02   ` Graham
2008-04-04 15:57     ` Adam Beneschan
2008-04-04 17:25     ` Robert A Duff
2008-04-04 19:37       ` Simon Wright

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