comp.lang.ada
 help / color / mirror / Atom feed
* Private type definition must be definite : but why ?
@ 2008-03-02  0:23 Hibou57 (Yannick Duchêne)
  2008-03-02  0:37 ` Hibou57 (Yannick Duchêne)
  2008-03-02  0:43 ` Robert A Duff
  0 siblings, 2 replies; 4+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2008-03-02  0:23 UTC (permalink / raw)


Hello, hello...

I'm trying to express something I can't express with Ada semantic :

> package Xxx
>   type Data_Row_Type is private;
>   ...
> private
>   type Data_Row_Type is new String;
>   ...
> end Xxx;

Bu I cannot do that, beceause a private type definition must be
definite.

While I can do

> package Xxx
>   type Data_Row_Type is new String;
>   ...
> end Xxx;

I would like to understand the reason of this restriction. There is a
user view (the public part) and a compiler view (public part + private
part). So what's the matter if I want the user not to rely on the fact
that Data_Row_Type is a new String type, but still want the compiler
to know it. Why is it not allowed ?

What does justify the obligation for a private type to be definite ?

I do not see any reason at the time.

Thanks and good times to you

Yannick



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

* Re: Private type definition must be definite : but why ?
  2008-03-02  0:23 Private type definition must be definite : but why ? Hibou57 (Yannick Duchêne)
@ 2008-03-02  0:37 ` Hibou57 (Yannick Duchêne)
  2008-03-03 16:29   ` Adam Beneschan
  2008-03-02  0:43 ` Robert A Duff
  1 sibling, 1 reply; 4+ messages in thread
From: Hibou57 (Yannick Duchêne) @ 2008-03-02  0:37 UTC (permalink / raw)


Oops, sorry.... I was to much speedy to see a trouble here.

I just have to use an unknown discriminant part (beceause it is
private), thus just have to do

> package Xxx
>   type Data_Row_Type(<>) is private;
>   ...
> private
>    type Data_Row_Type is new String;
>   ...
> end Xxx;

And it works, and every body's happy... me first :D

Well, to be honest, I still do not understand why the user have to
know there is are unknow discriminants somewhere..... the user does
not need to know.



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

* Re: Private type definition must be definite : but why ?
  2008-03-02  0:23 Private type definition must be definite : but why ? Hibou57 (Yannick Duchêne)
  2008-03-02  0:37 ` Hibou57 (Yannick Duchêne)
@ 2008-03-02  0:43 ` Robert A Duff
  1 sibling, 0 replies; 4+ messages in thread
From: Robert A Duff @ 2008-03-02  0:43 UTC (permalink / raw)


""Hibou57 (Yannick Duch�ne)"" <yannick_duchene@yahoo.fr> writes:

> Hello, hello...
>
> I'm trying to express something I can't express with Ada semantic :
>
>> package Xxx
>>   type Data_Row_Type is private;
>>   ...
>> private
>>   type Data_Row_Type is new String;
>>   ...
>> end Xxx;
>
> Bu I cannot do that, beceause a private type definition must be
> definite.

You can say "X : Integer;" but you cannot say "X : String;",
because String is indefinite -- we don't know its size.

If the above were legal, then you could say "X : Xxx.Data_Row_Type;".
(In clients of Xxx, I mean.)  But that won't work -- we don't know its
size.  It can't be illegal, because legality rules should not break
privacy.

One solution is to use "unknown discriminants":

package Xxx
  type Data_Row_Type (<>) is private;
  ...
private
  type Data_Row_Type is new String;
  ...
end Xxx;

This means that the full type might have unconstrained bounds or
discriminants.  (Array bounds are conceptually pretty-much the same
thing as discriminants.)  Now the private type is indefinite,
So now "X : Xxx.Data_Row_Type;" is illegal -- without breaking
privacy.

Presumably you would have some constructor functions,
so the client can say "X : Xxx.Data_Row_Type := Make_Data_Row (...);".

> While I can do
>
>> package Xxx
>>   type Data_Row_Type is new String;
>>   ...
>> end Xxx;
>
> I would like to understand the reason of this restriction. There is a
> user view (the public part) and a compiler view (public part + private
> part).

What you call the "compiler view" is for code generation.
Ada is designed so that the compiler need not peek at
the private part for legality rules.  That's a good thing,
because it allows the programmer of Xxx to change the private
part without worrying about breaking clients.

- Bob



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

* Re: Private type definition must be definite : but why ?
  2008-03-02  0:37 ` Hibou57 (Yannick Duchêne)
@ 2008-03-03 16:29   ` Adam Beneschan
  0 siblings, 0 replies; 4+ messages in thread
From: Adam Beneschan @ 2008-03-03 16:29 UTC (permalink / raw)


On Mar 1, 4:37 pm, "Hibou57 (Yannick Duchêne)"
<yannick_duch...@yahoo.fr> wrote:
> Oops, sorry.... I was to much speedy to see a trouble here.
>
> I just have to use an unknown discriminant part (beceause it is
> private), thus just have to do
>
> > package Xxx
> >   type Data_Row_Type(<>) is private;
> >   ...
> > private
> >    type Data_Row_Type is new String;
> >   ...
> > end Xxx;
>
> And it works, and every body's happy... me first :D
>
> Well, to be honest, I still do not understand why the user have to
> know there is are unknow discriminants somewhere..... the user does
> not need to know.

The "user" has to know that there are unknown *constraints* (which
could be discriminants, or could, as in this case, be array
constraints), so that the user knows that you can't declare a variable
(or other object) of that type without adding constraints.  This is
illegal:

    X : String;

since you have to put array constraints on it.  And if your program
looks like this:

    package Xxx is
        type Data_Row_Type is private;
    private
        DO NOT LOOK IN HERE
    end Xxx;

    with Xxx;
    package Yyy is
        Z : Xxx.Data_Row_Type;
    end Yyy;

If Data_Row_Type could be a "new String", you wouldn't be able to tell
that Z is illegal without looking in the private part of Xxx, and
you're not allowed to look at that.  That's what Bob referred to as
"breaking privacy".

In this case:

    package Xxx is
        type Data_Row_Type(<>) is private;
    private
        DO NOT LOOK IN HERE
    end Xxx;

    with Xxx;
    package Yyy is
        Z : Xxx.Data_Row_Type;  -- ILLEGAL
    end Yyy;

Now Z is illegal, and the "user" can tell that without having to look
in the private part of Xxx.  That would make it OK for Data_Row_Type
to be declared as a String.

When you declare a private type, you'll need to ask: Does your design
require that users of the package declare objects of that type?  If
the answer is "no", then you should put (<>) on the declaration so
that you can make the private type be anything you want.

P.S. If you really need Data_Row_Type to be a string and you need your
users to be able to declare objects of the type, consider
Ada.Strings.Bounded or Ada.Strings.Unbounded.

                                   -- Adam








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

end of thread, other threads:[~2008-03-03 16:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-02  0:23 Private type definition must be definite : but why ? Hibou57 (Yannick Duchêne)
2008-03-02  0:37 ` Hibou57 (Yannick Duchêne)
2008-03-03 16:29   ` Adam Beneschan
2008-03-02  0:43 ` Robert A Duff

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