comp.lang.ada
 help / color / mirror / Atom feed
From: "Ludovic Brenta" <ludovic@ludovic-brenta.org>
Subject: Re: Instantiating private types with discriminants?
Date: 10 May 2006 08:45:07 -0700
Date: 2006-05-10T08:45:07-07:00	[thread overview]
Message-ID: <1147275907.235722.316850@e56g2000cwe.googlegroups.com> (raw)
In-Reply-To: <%nn8g.29165$_k2.508499@news2.nokia.com>

rick H a écrit :
> with Ada.Integer_Text_IO;
> with Ada.Float_Text_IO;
>
> procedure Simple_Case is
>
>    type General_T is tagged null record;
>    type Access_T is access General_T'Class;
>
>    type Type_A is new General_T with
>    record
>       Data : Integer;
>    end record;
>
>    type Type_B is new General_T with
>    record
>       Data : Float;
>    end record;
>
>    procedure Put (Item: in General_T) is
>    begin
>       null;
>    end Put;
>
>    procedure Put (Item: in Type_A) is
>    begin
>       Ada.Integer_Text_IO.Put (Item.Data);
>    end Put;
>
>    procedure Put (Item: in Type_B) is
>    begin
>       Ada.Float_Text_IO.Put (Item.Data);
>    end Put;
>
>    Var_A : Access_T; --  could be "new Type_A" or a "new Type_B"
> begin
>
>    --  Var_A := new Type_A' (Data => 100);
>    Var_A := new Type_B' (Data => 100.0);
>
>    Put (Var_A.all);
> end Simple_Case;

First , per ARM 3.2.3, you should declare the three types and the Put
procedures inside a package (possibly nested in the Simple_Case
procedure), so that they fall under ARM 3.2.3(6). Otherwise, the Put
procedures are not primitive, and therefore not dispatching.

Secondly, you are being hit by what may be Ada's most subtle rules:
freezing rules (ARM 13.14).

The declaration of type Type_A freezes General_T, per ARM 13.14(7).
This means that after the declaration of Type_A, you can no longer
declare primitive subprograms of General_T; any subprograms you declare
thereafter are non-primitive, and so dynamic dispatching is forbidden.
This is an opportunity to praise GNAT's excellent warnings:

dispatch.adb:11:07: warning: no primitive operations for "General_T"
after this line
(this is the line declaring Type_A)

dispatch.adb:19:17: this primitive operation is declared too late
(this is the line declaring Put (General_T)

dispatch.adb:52:15: class-wide argument not allowed here
dispatch.adb:52:15: "Put" is not a primitive operation of "General_T"
(quite explicit when you know the language rules)

Move the declaration of Put (General_Type) before the declaration of
Type_A, and the declaration of Put (Type_A) before the declaration of
Type_B (which freezes Type_A). This will make these subprograms
primitive.

The basic idea behind freezing rules is that you cannot add a primitive
operation to the vtable of a tagged type after declaring another type
derived from it.

-- 
Ludovic Brenta.




  reply	other threads:[~2006-05-10 15:45 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-09 13:17 Instantiating private types with discriminants? rick H
2006-05-09 13:45 ` Georg Bauhaus
2006-05-09 14:06   ` rick H
2006-05-09 13:56 ` Ludovic Brenta
2006-05-09 14:24   ` rick H
2006-05-09 19:48     ` Ludovic Brenta
2006-05-09 14:05 ` Dmitry A. Kazakov
2006-05-09 14:48 ` rick H
2006-05-09 15:20   ` Jerry Petrey
2006-05-09 15:42     ` rick H
2006-05-09 15:53   ` Avoiding use Ada.Tags (was Re: Instantiating private types with discriminants?) Alex R. Mosteo
2006-05-09 16:01   ` Instantiating private types with discriminants? Dmitry A. Kazakov
2006-05-10  7:42     ` rick H
2006-05-10  9:09       ` Ludovic Brenta
2006-05-10 11:49         ` Georg Bauhaus
2006-05-10 13:44         ` rick H
2006-05-10 14:21           ` Ludovic Brenta
2006-05-10 15:10             ` rick H
2006-05-10 15:45               ` Ludovic Brenta [this message]
2006-05-10 14:41           ` Dmitry A. Kazakov
2006-05-10 15:34             ` rick H
2006-05-10 19:01               ` Georg Bauhaus
2006-05-10 19:05                 ` Ludovic Brenta
2006-05-10 21:52                   ` Rick H
2006-05-11  1:17                     ` Jeffrey R. Carter
2006-05-11  7:44                     ` Dmitry A. Kazakov
2006-05-11  8:27                       ` rick H
2006-05-11 10:28                         ` Dmitry A. Kazakov
2006-05-11 15:59                           ` Robert A Duff
2006-05-12  7:37                             ` Dmitry A. Kazakov
2006-05-12  9:24                               ` Georg Bauhaus
2006-05-12 12:40                                 ` Dmitry A. Kazakov
2006-05-12 18:25                                   ` Randy Brukardt
2006-05-09 19:57   ` "Use" and "=" for Tags (was: Re: Instantiating private types with discriminants?) Jeffrey R. Carter
replies disabled

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