comp.lang.ada
 help / color / mirror / Atom feed
* Inheritance with Ada types
@ 2010-02-09  2:16 Bryan
  2010-02-09  2:57 ` Hibou57 (Yannick Duchêne)
  2010-02-09 15:08 ` Robert A Duff
  0 siblings, 2 replies; 9+ messages in thread
From: Bryan @ 2010-02-09  2:16 UTC (permalink / raw)


I'm new to the OOP features of Ada and I'm trying to wrap my head
tagged types.  I've been reading the somewhat abstruse "Ada for
Software Engineers" and while I understand the examples in the text, I
would like to experiment on my own to further my understand.  I'm
trying to figure out how I can correctly--pardon my C++--"subclass" a
base class to create a new derived "class".  I created two simple
packges as follows:

parent.ads:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package Parent is
  type Parent is abstract tagged private;
private
  type Parent is abstract tagged
    record
      Name: Unbounded_String;
    end record;
end Parent;

derived.ads:

with Parent; use Parent;
package Derived is
  type Derived is new Parent with private;
private
  type Derived is new Parent with
    record
      Sub_Name: Unbounded_String;
    end record;
end Derived;

I can compile parent.ads and the object file and ALI file are
produced.  I'm unable to get derived.ads to compile, however.  I get
the following error:

$ gnatmake -c parent derived.ads
gcc -c derived.ads
derived.ads:3:23: subtype mark required in this context
derived.ads:3:23: found "Parent" declared at parent.ads:2
derived.ads:5:23: subtype mark required in this context
derived.ads:5:23: found "Parent" declared at parent.ads:2
gnatmake: "derived.ads" compilation error

If I make the Derived type in a child package of the parent package, I
can get both interfaces to compile.  Perhaps I am misunderstanding
something about Ada OOP, but is it possible to have a derived package
that is not a child package of the base?  Must I use a child package,
or keep the derived type in the same package as the base type?



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

* Re: Inheritance with Ada types
  2010-02-09  2:16 Inheritance with Ada types Bryan
@ 2010-02-09  2:57 ` Hibou57 (Yannick Duchêne)
  2010-02-09  9:11   ` Jean-Pierre Rosen
  2010-02-09 15:08 ` Robert A Duff
  1 sibling, 1 reply; 9+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2010-02-09  2:57 UTC (permalink / raw)


On 9 fév, 03:16, Bryan <brobinson....@gmail.com> wrote:
> I created two simple
> packges as follows:
>
> parent.ads:
>
> with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
> package Parent is
>   type Parent is abstract tagged private;
> private
>   type Parent is abstract tagged
>     record
>       Name: Unbounded_String;
>     end record;
> end Parent;
>
> derived.ads:
>
> with Parent; use Parent;
> package Derived is
>   type Derived is new Parent with private;
> private
>   type Derived is new Parent with
>     record
>       Sub_Name: Unbounded_String;
>     end record;
> end Derived;
Oops : package and type name share the same name and you there are use
clause... the use clause is particularly evil here.

> $ gnatmake -c parent derived.ads
> gcc -c derived.ads
> derived.ads:3:23: subtype mark required in this context
> derived.ads:3:23: found "Parent" declared at parent.ads:2
The message refer to
> package Parent is

> derived.ads:5:23: subtype mark required in this context
> derived.ads:5:23: found "Parent" declared at parent.ads:2
The same as above

Either rename your packages (like Parents and Deriveds ... although
the latter is not correct English) or drop the use clause and refer to
your Parent ancestor as Parent.Parent instead.

By the way, the With clause for Ada.Strings.Unbounded is missing in
Derived.ads



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

* Re: Inheritance with Ada types
  2010-02-09  2:57 ` Hibou57 (Yannick Duchêne)
@ 2010-02-09  9:11   ` Jean-Pierre Rosen
  2010-02-09 10:19     ` Hibou57 (Yannick Duchêne)
  2010-02-09 10:22     ` Hibou57 (Yannick Duchêne)
  0 siblings, 2 replies; 9+ messages in thread
From: Jean-Pierre Rosen @ 2010-02-09  9:11 UTC (permalink / raw)


Hibou57 (Yannick Duch�ne) a �crit :
> On 9 f�v, 03:16, Bryan <brobinson....@gmail.com> wrote:
> Oops : package and type name share the same name and you there are use
> clause... the use clause is particularly evil here.
Why? The use clause has no effect in this case!

>> $ gnatmake -c parent derived.ads
>> gcc -c derived.ads
>> derived.ads:3:23: subtype mark required in this context
>> derived.ads:3:23: found "Parent" declared at parent.ads:2
> The message refer to
>> package Parent is
> 
>> derived.ads:5:23: subtype mark required in this context
>> derived.ads:5:23: found "Parent" declared at parent.ads:2
> The same as above
> 
> Either rename your packages (like Parents and Deriveds ... although
> the latter is not correct English) 
or use the convention of giving the class name to the package, and some
fixed name like "object" or "instance" to the type. Full rationale for
this notation is available in my paper "A naming convention for classes
in Ada 9X", downloadable from http://www.adalog.fr/publica2.htm

> or drop the use clause and refer to
> your Parent ancestor as Parent.Parent instead.
Of course, you don't need to drop the use clause to write Parent.Parent!

I don't understand why people think that as soon as you have a use
clause, you cannot use full names. If within the scope of a use clause
you don't get what you want, or if you find that in some cases full
names are more readable, by all mean, use full notation!

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



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

* Re: Inheritance with Ada types
  2010-02-09  9:11   ` Jean-Pierre Rosen
@ 2010-02-09 10:19     ` Hibou57 (Yannick Duchêne)
  2010-02-09 11:23       ` Jean-Pierre Rosen
  2010-02-09 10:22     ` Hibou57 (Yannick Duchêne)
  1 sibling, 1 reply; 9+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2010-02-09 10:19 UTC (permalink / raw)


On 9 fév, 10:11, Jean-Pierre Rosen <ro...@adalog.fr> wrote:
> > clause... the use clause is particularly evil here.
With package named Parent, containing a type named Parent, having a
use clause on Parent brings into a context where the name Package can
be both resolved as a package or as a type.

The use clause is clearly involved here, as one the way to get ride of
this error is to remove the use clause. The other way being to rename
either the type either the package.

May be I was wrong to say the use clause is particularly evil there :
I should have said “ the use clause is particularly vicious there
” (due to its vicious side effect in such a kind of context).




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

* Re: Inheritance with Ada types
  2010-02-09  9:11   ` Jean-Pierre Rosen
  2010-02-09 10:19     ` Hibou57 (Yannick Duchêne)
@ 2010-02-09 10:22     ` Hibou57 (Yannick Duchêne)
  1 sibling, 0 replies; 9+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2010-02-09 10:22 UTC (permalink / raw)


On 9 fév, 10:11, Jean-Pierre Rosen <ro...@adalog.fr> wrote:
> I don't understand why people think that as soon as you have a use
> clause, you cannot use full names. If within the scope of a use clause
> you don't get what you want, or if you find that in some cases full
> names are more readable, by all mean, use full notation!
Agree with this. True that having a Use clause does not implies the
prefix notation is not available anymore.

Well, let say in this context he should not rely on the use clause to
refer to the type Parent.



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

* Re: Inheritance with Ada types
  2010-02-09 10:19     ` Hibou57 (Yannick Duchêne)
@ 2010-02-09 11:23       ` Jean-Pierre Rosen
  2010-02-10  2:47         ` Randy Brukardt
  0 siblings, 1 reply; 9+ messages in thread
From: Jean-Pierre Rosen @ 2010-02-09 11:23 UTC (permalink / raw)


Hibou57 (Yannick Duch�ne) a �crit :
> On 9 f�v, 10:11, Jean-Pierre Rosen <ro...@adalog.fr> wrote:
>>> clause... the use clause is particularly evil here.
> With package named Parent, containing a type named Parent, having a
> use clause on Parent brings into a context where the name Package can
> be both resolved as a package or as a type.
> 
> The use clause is clearly involved here, as one the way to get ride of
> this error is to remove the use clause. The other way being to rename
> either the type either the package.
> 
> May be I was wrong to say the use clause is particularly evil there :
> I should have said � the use clause is particularly vicious there
> � (due to its vicious side effect in such a kind of context).
> 
I don't see anything vicious. You have to understand that the use clause
is "weak", it never goes against normal visibility. You have a package
named Parent which is directly visible, a type named Parent which is
use-visible. In this case, the use clause politely gives the way to
direct visibility.

The evil/vicious/whatever is in having nested entities with the same name.

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



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

* Re: Inheritance with Ada types
  2010-02-09  2:16 Inheritance with Ada types Bryan
  2010-02-09  2:57 ` Hibou57 (Yannick Duchêne)
@ 2010-02-09 15:08 ` Robert A Duff
  2010-02-09 23:39   ` Bryan
  1 sibling, 1 reply; 9+ messages in thread
From: Robert A Duff @ 2010-02-09 15:08 UTC (permalink / raw)


Bryan <brobinson.eng@gmail.com> writes:

> package Parent is
>   type Parent is abstract tagged private;

Using the same name for the package and the type will lead to confusion.
A common convention is to use a plural for the package, and a singular
for the main type in that package.  "Parent" is a fairly meaningless
name, but in a real program you'd have something like:

    package Widgets is
        type Widget is ...

- Bob



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

* Re: Inheritance with Ada types
  2010-02-09 15:08 ` Robert A Duff
@ 2010-02-09 23:39   ` Bryan
  0 siblings, 0 replies; 9+ messages in thread
From: Bryan @ 2010-02-09 23:39 UTC (permalink / raw)


Thanks everyone for correcting my code.  Changing the package name
cleared up the errors.  I should have looked harder at the package
names from the book.  I'll probably be back with more questions when
I'm stuck again.  Thanks for the help.



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

* Re: Inheritance with Ada types
  2010-02-09 11:23       ` Jean-Pierre Rosen
@ 2010-02-10  2:47         ` Randy Brukardt
  0 siblings, 0 replies; 9+ messages in thread
From: Randy Brukardt @ 2010-02-10  2:47 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1565 bytes --]

"Jean-Pierre Rosen" <rosen@adalog.fr> wrote in message 
news:tjgrkh.el7.ln@hunter.axlog.fr...
> Hibou57 (Yannick Duch�ne) a �crit :
>> On 9 f�v, 10:11, Jean-Pierre Rosen <ro...@adalog.fr> wrote:
>>>> clause... the use clause is particularly evil here.
>> With package named Parent, containing a type named Parent, having a
>> use clause on Parent brings into a context where the name Package can
>> be both resolved as a package or as a type.
>>
>> The use clause is clearly involved here, as one the way to get ride of
>> this error is to remove the use clause. The other way being to rename
>> either the type either the package.
>>
>> May be I was wrong to say the use clause is particularly evil there :
>> I should have said � the use clause is particularly vicious there
>> � (due to its vicious side effect in such a kind of context).
>>
> I don't see anything vicious. You have to understand that the use clause
> is "weak", it never goes against normal visibility. You have a package
> named Parent which is directly visible, a type named Parent which is
> use-visible. In this case, the use clause politely gives the way to
> direct visibility.
>
> The evil/vicious/whatever is in having nested entities with the same name.

I suspect that the "evil" thing here is that the programmer is expecting the 
use clause to have some effect, but it does not. And that is confusing 
(although it is the confusing naming that is the real problem, not the 
presence or absence of the use clause).

                                              Randy.





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

end of thread, other threads:[~2010-02-10  2:47 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-09  2:16 Inheritance with Ada types Bryan
2010-02-09  2:57 ` Hibou57 (Yannick Duchêne)
2010-02-09  9:11   ` Jean-Pierre Rosen
2010-02-09 10:19     ` Hibou57 (Yannick Duchêne)
2010-02-09 11:23       ` Jean-Pierre Rosen
2010-02-10  2:47         ` Randy Brukardt
2010-02-09 10:22     ` Hibou57 (Yannick Duchêne)
2010-02-09 15:08 ` Robert A Duff
2010-02-09 23:39   ` Bryan

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