comp.lang.ada
 help / color / mirror / Atom feed
* problems with classes
@ 2004-09-30 21:20 Rick Santa-Cruz
  2004-10-01  7:35 ` Dmitry A. Kazakov
  2004-10-01  7:58 ` Martin Krischik
  0 siblings, 2 replies; 6+ messages in thread
From: Rick Santa-Cruz @ 2004-09-30 21:20 UTC (permalink / raw)


Hi,

First of all I wanna thank all people in this group for the help. I am just 
a beginner in Ada and before I started Ada, I thought that cause I know C++ 
very well, it would be easy for me to understand Ada (in fact I made this 
experience with learning programmin languages like Java, Perl, C#, etc. 
after working a lot with C++). But this assumption was totally wrong. The 
syntax of Ada seems to me a bit strange, at least at the moment ;), but I am 
working hard to become better in Ada...

Here are 2 examples of source-codes:
1. Source1:
package Source_1 is
    type Parent_Class is tagged
        record
            Number: Integer;
        end record;
    type Child_Class is new Parent_Class with private;

    procedure Parent_Proc(P: Parent_Class);
    procedure Child_Proc(C: Child_Class);

    private
        type Child_Class is new Parent_Class with
            record
                Number2: Integer;
            end record;
end Source_1;
---------------------------------------------------------------------
1. Source2:
package Source_2 is
    type Parent_Class is tagged
        record
            Number: Integer;
        end record;
    type Child_Class is new Parent_Class with
            record
                Number2: Integer;
            end record;

    procedure Parent_Proc(P: Parent_Class);
    procedure Child_Proc(C: Child_Class);
end Source_2;

My question is now, why in the seconde code-example I get an error, that 
Parent_Class has to be declared ready, before I define the Child_Class? Why 
I don't get this error in the first case, cause there I specify the 
Child_Class with: "type Child_Class is new Parent_Class with private;", 
before I declare the procedure of the class Parent_Class?
If this would be C++, I think I would know the answer... so in C++ 
"something like this" exists, too. It is called pre-definition of a type. 
That means that, for example if I need the name of a type before I wanna 
specify it, I can just write:
class MyType;
and specify it later. Is this the case in Ada too?

Thanks in advance,
Rick 





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

* Re: problems with classes
  2004-09-30 21:20 problems with classes Rick Santa-Cruz
@ 2004-10-01  7:35 ` Dmitry A. Kazakov
  2004-10-01 18:41   ` Rick Santa-Cruz
  2004-10-01  7:58 ` Martin Krischik
  1 sibling, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2004-10-01  7:35 UTC (permalink / raw)


On Thu, 30 Sep 2004 23:20:47 +0200, Rick Santa-Cruz wrote:

> First of all I wanna thank all people in this group for the help. I am just 
> a beginner in Ada and before I started Ada, I thought that cause I know C++ 
> very well, it would be easy for me to understand Ada (in fact I made this 
> experience with learning programmin languages like Java, Perl, C#, etc. 
> after working a lot with C++). But this assumption was totally wrong. The 
> syntax of Ada seems to me a bit strange, at least at the moment ;), but I am 
> working hard to become better in Ada...
> 
> Here are 2 examples of source-codes:
> 1. Source1:
> package Source_1 is
>     type Parent_Class is tagged
>         record
>             Number: Integer;
>         end record;
>     type Child_Class is new Parent_Class with private;
> 
>     procedure Parent_Proc(P: Parent_Class);
>     procedure Child_Proc(C: Child_Class);
> 
>     private
>         type Child_Class is new Parent_Class with
>             record
>                 Number2: Integer;
>             end record;
> end Source_1;
> ---------------------------------------------------------------------
> 1. Source2:
> package Source_2 is
>     type Parent_Class is tagged
>         record
>             Number: Integer;
>         end record;
>     type Child_Class is new Parent_Class with
>             record
>                 Number2: Integer;
>             end record;
> 
>     procedure Parent_Proc(P: Parent_Class);
>     procedure Child_Proc(C: Child_Class);
> end Source_2;
> 
> My question is now, why in the seconde code-example I get an error, that 
> Parent_Class has to be declared ready, before I define the Child_Class? Why 
> I don't get this error in the first case, cause there I specify the 
> Child_Class with: "type Child_Class is new Parent_Class with private;", 
> before I declare the procedure of the class Parent_Class?

In the first case the type Child_Class is not fully defined at the
declaration point of Parent_Proc. In the second case it is, which in effect
freezes Parent_Class, so that Parent_Proc cannot be declared as a primitive
operation. You can modify Source_2 as follows:

package Source_2 is
   type Parent_Class is tagged
      record
         Number: Integer;
      end record;
   procedure Parent_Proc(P: Parent_Class); -- Before freezing point, now

   type Child_Class is new Parent_Class with
      record
         Number2: Integer;
      end record;
   procedure Child_Proc(C: Child_Class);
end Source_2;

> If this would be C++, I think I would know the answer... so in C++ 
> "something like this" exists, too. It is called pre-definition of a type. 
> That means that, for example if I need the name of a type before I wanna 
> specify it, I can just write:
> class MyType; and specify it later. Is this the case in Ada too?

Close, but not quite. In your example you define the public view of a type
and later define its implementation. What you meant is a forward type
declaration, it looks exactly like in C++:

type Something; -- Forward declaration

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



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

* Re: problems with classes
  2004-09-30 21:20 problems with classes Rick Santa-Cruz
  2004-10-01  7:35 ` Dmitry A. Kazakov
@ 2004-10-01  7:58 ` Martin Krischik
  1 sibling, 0 replies; 6+ messages in thread
From: Martin Krischik @ 2004-10-01  7:58 UTC (permalink / raw)


Rick Santa-Cruz wrote:

> Hi,
> 
> First of all I wanna thank all people in this group for the help. I am
> just a beginner in Ada and before I started Ada, I thought that cause I
> know C++ very well, it would be easy for me to understand Ada (in fact I
> made this experience with learning programmin languages like Java, Perl,
> C#, etc. after working a lot with C++). But this assumption was totally
> wrong. The syntax of Ada seems to me a bit strange, at least at the moment
> ;), but I am working hard to become better in Ada...
> 
> Here are 2 examples of source-codes:
> 1. Source1:
> package Source_1 is
>     type Parent_Class is tagged
>         record
>             Number: Integer;
>         end record;
>     type Child_Class is new Parent_Class with private;
> 
>     procedure Parent_Proc(P: Parent_Class);
>     procedure Child_Proc(C: Child_Class);
> 
>     private
>         type Child_Class is new Parent_Class with
>             record
>                 Number2: Integer;
>             end record;
> end Source_1;
> ---------------------------------------------------------------------
> 1. Source2:
> package Source_2 is
>     type Parent_Class is tagged
>         record
>             Number: Integer;
>         end record;
>     type Child_Class is new Parent_Class with
>             record
>                 Number2: Integer;
>             end record;
> 
>     procedure Parent_Proc(P: Parent_Class);
>     procedure Child_Proc(C: Child_Class);
> end Source_2;
> 
> My question is now, why in the seconde code-example I get an error, that
> Parent_Class has to be declared ready, before I define the Child_Class?
> Why I don't get this error in the first case, cause there I specify the
> Child_Class with: "type Child_Class is new Parent_Class with private;",
> before I declare the procedure of the class Parent_Class?
> If this would be C++, I think I would know the answer... so in C++
> "something like this" exists, too. It is called pre-definition of a type.
> That means that, for example if I need the name of a type before I wanna
> specify it, I can just write:
> class MyType;
> and specify it later. Is this the case in Ada too?

Because die declared Parent_Proc after the "full view" of Child_Class. To
declare the "full view" of Child_Class Parent_Proc need to be "frozen".

When a type is "frozen" you can't make any changes to it. And for a class
that means, you can't add another method.

Try:

package Source_2 is
    type Parent_Class is tagged
        record
            Number: Integer;
        end record;

    procedure Parent_Proc(P: Parent_Class);

    type Child_Class is new Parent_Class with
            record
                Number2: Integer;
            end record;

     procedure Child_Proc(C: Child_Class);
end Source_2;

And:

package Source_1 is
     type Parent_Class is tagged private;
     type Child_Class is new Parent_Class with private;
 
    procedure Parent_Proc(P: Parent_Class);
    procedure Child_Proc(C: Child_Class);
 
private
    type Parent_Class is tagged
        record
            Number: Integer;
        end record;

        type Child_Class is new Parent_Class with
            record
                Number2: Integer;
            end record;
end Source_1;

And see what happens.

Last not least: Usually one defines only one class per package. That's
unlike C++ where tons of classes share one namespace.

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com




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

* Re: problems with classes
  2004-10-01  7:35 ` Dmitry A. Kazakov
@ 2004-10-01 18:41   ` Rick Santa-Cruz
  2004-10-04  8:33     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 6+ messages in thread
From: Rick Santa-Cruz @ 2004-10-01 18:41 UTC (permalink / raw)


Hi,

>
>> If this would be C++, I think I would know the answer... so in C++
>> "something like this" exists, too. It is called pre-definition of a type.
>> That means that, for example if I need the name of a type before I wanna
>> specify it, I can just write:
>> class MyType; and specify it later. Is this the case in Ada too?
>
> Close, but not quite. In your example you define the public view of a type
> and later define its implementation. What you meant is a forward type
> declaration, it looks exactly like in C++:
Yes forward declaration ;).

But if "type Child_Class is new Parent_Class with private;" is not a forward 
declaration then what is it then, or what is the sense of this soruce-line?

Thanks,
Rick 





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

* Re: problems with classes
  2004-10-01 18:41   ` Rick Santa-Cruz
@ 2004-10-04  8:33     ` Dmitry A. Kazakov
  2004-10-04 10:19       ` Martin Dowie
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2004-10-04  8:33 UTC (permalink / raw)


On Fri, 1 Oct 2004 20:41:50 +0200, Rick Santa-Cruz wrote:

>>> If this would be C++, I think I would know the answer... so in C++
>>> "something like this" exists, too. It is called pre-definition of a type.
>>> That means that, for example if I need the name of a type before I wanna
>>> specify it, I can just write:
>>> class MyType; and specify it later. Is this the case in Ada too?
>>
>> Close, but not quite. In your example you define the public view of a type
>> and later define its implementation. What you meant is a forward type
>> declaration, it looks exactly like in C++:
> Yes forward declaration ;).
> 
> But if "type Child_Class is new Parent_Class with private;" is not a forward 
> declaration then what is it then, or what is the sense of this soruce-line?

To publish the contract. It is a declaration of the public interface of the
type Child_Class. It states:

1. There is a type Child_Class
2. It is a descendant of Parent_Class (maybe an indirect one)

The clients of the package can rely on this contract. Forward declaration
(incomplete type declaration) does not define any completed contract. So
the following is illegal:

package Foo is -- ILLEGAL
   type X; -- Incomplete declaration
private
   type X is range 1..20; -- Type completion, "full view"

It makes no sense for public clients. No public view of X is defined, but
they cannot look into private. Compare with:

package Foo is -- LEGAL
   type X is private; -- True contract, "public view"
private
   type X is range 1..20; -- Type completion, "full view"

Also illegal is:

package Foo is -- ILLEGAL
   type X; -- Promise to complete it here
   type X is private; -- No, I will not

You cannot complete X in the private part, because you have promised that X
will be completed in the public part. So either:

package Foo is -- LEGAL
   ...
private
   type X; -- Incomplete declaration
   type X is range 1..20; -- Type completion and full view

or

package Foo is -- LEGAL
   type X; -- Incomplete declaration
   type X is range 1..20; -- Type completion and full view
private
   ...

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



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

* Re: problems with classes
  2004-10-04  8:33     ` Dmitry A. Kazakov
@ 2004-10-04 10:19       ` Martin Dowie
  0 siblings, 0 replies; 6+ messages in thread
From: Martin Dowie @ 2004-10-04 10:19 UTC (permalink / raw)


Dmitry A. Kazakov wrote:
> You cannot complete X in the private part, because you have promised
> that X will be completed in the public part. So either:
>
> package Foo is -- LEGAL
>    ...
> private
>    type X; -- Incomplete declaration
>    type X is range 1..20; -- Type completion and full view
>
> or
>
> package Foo is -- LEGAL
>    type X; -- Incomplete declaration
>    type X is range 1..20; -- Type completion and full view
> private
>    ...

And don't forget...

package Foo is
private
   type X;  -- Incomplete declaration; an "opaque type"
end Foo;

package body Foo is
   type X is range 1 .. 20;  -- No public view
   ...
end Foo;





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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-30 21:20 problems with classes Rick Santa-Cruz
2004-10-01  7:35 ` Dmitry A. Kazakov
2004-10-01 18:41   ` Rick Santa-Cruz
2004-10-04  8:33     ` Dmitry A. Kazakov
2004-10-04 10:19       ` Martin Dowie
2004-10-01  7:58 ` Martin Krischik

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