comp.lang.ada
 help / color / mirror / Atom feed
* Problem with Enumaration and visibility
@ 2001-11-14 13:03 ` Preben Randhol
  2001-11-14 13:48   ` Wilhelm Spickermann
                     ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Preben Randhol @ 2001-11-14 13:03 UTC (permalink / raw)


Hi

I have defined a Enumarated type in a package called setup. I need to
use this in another package called Examine. If I do as below, I get this
error from GNAT:

   examine.adb:130:17: operator for type "Method_Enum_Type" defined at
   setup.ads:64 is not directly visible

   examine.adb:130:17: use clause would make operation legal
   examine.adb:130:47: "Spell" is not visible
   examine.adb:130:47: non-visible declaration at setup.ads:64
   gnatmake: "examine.adb" compilation error
   make: *** [all] Error 4
   (3 of 8): 17: operator for type "Method_Enum_Type" defined at setup.ads:64 
   is not directly visible

if I add "use Setup;" in the Start procedure it works. Why do I have
to do this? Is there a different way without using use. I'm just
asking to understand how this works with enumarated types.

Thanks in advance.


-----setup.ads---
package Setup is
-- ...

type Method_Enum_Type is (Spell, MChoice);

procedure Get_Method return Method_Enum_Type;
-- ...
end Setup;

-----examine.adb---
with Setup;

package body Examine is

-- ...

   procedure Start is
      Method : Setup.Method_Enum_Type := Setup.Get_Method;
   begin
      if Method = Setup.Method_Enum_Type'(Spell) then
         Examine.Spelling.Start;
      end if;
   end Start;

-- ...

end Examine;
-- 
Please, stop bombing civilians in Afghanistan. One cannot write off
killing innocent children and other civilians as "collateral damage".
A civilian is a civilian whether he or she is American or from another
country in the world.           http://web.amnesty.org/11september.htm



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

* Re: Problem with Enumaration and visibility
  2001-11-14 13:03 ` Preben Randhol
@ 2001-11-14 13:48   ` Wilhelm Spickermann
  2001-11-14 16:55     ` Preben Randhol
  2001-11-14 15:28   ` Ted Dennison
  2001-11-14 16:32   ` Jeffrey Carter
  2 siblings, 1 reply; 7+ messages in thread
From: Wilhelm Spickermann @ 2001-11-14 13:48 UTC (permalink / raw)
  To: comp.lang.ada



--On Mittwoch, November 14, 2001 13:03:19 +0000 Preben Randhol 
<randhol+abuse@pvv.org> wrote:

...
>       if Method = Setup.Method_Enum_Type'(Spell) then
                  ^                         ^^^^^

These are two items which are defined in Setup and need a 
"Setup." too, if you want to avoid using "use". This leads to:

...
if Setup."=" (Method, Setup.Method_Enum_Type'(Setup.Spell)) then
...

which can be simplified to:

...
if Setup."=" (Method, Setup.Spell) then
...

The ugly ´Setup."="´ can be avoided by a "use type" clause:

...
      use type Setup.Method_Enum_Type;
      Method : Setup.Method_Enum_Type := Setup.Get_Method;
   begin
      if Method = Setup.Spell then
...

Wilhelm




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

* Re: Problem with Enumaration and visibility
  2001-11-14 13:03 ` Preben Randhol
  2001-11-14 13:48   ` Wilhelm Spickermann
@ 2001-11-14 15:28   ` Ted Dennison
  2001-11-14 16:32   ` Jeffrey Carter
  2 siblings, 0 replies; 7+ messages in thread
From: Ted Dennison @ 2001-11-14 15:28 UTC (permalink / raw)


In article <slrn9v4uqg.h34.randhol+abuse@kiuk0156.chembio.ntnu.no>, Preben
Randhol says...
>I have defined a Enumarated type in a package called setup. I need to
>use this in another package called Examine. If I do as below, I get this
>error from GNAT:
>
>   examine.adb:130:17: operator for type "Method_Enum_Type" defined at
>   setup.ads:64 is not directly visible
>
>   examine.adb:130:17: use clause would make operation legal
>   examine.adb:130:47: "Spell" is not visible
>   examine.adb:130:47: non-visible declaration at setup.ads:64
..
>if I add "use Setup;" in the Start procedure it works. Why do I have
>to do this? Is there a different way without using use. I'm just
>asking to understand how this works with enumarated types.

First off, this is a *really* basic issue, which would take a good deal of
exposition to explain properly. I suggest you read an Ada book rather than have
people here try to get into it. Chapter 2 of John English's book goes into this,
if you don't have your own dead-tree book handy. See
http://www.it.bton.ac.uk/staff/je/adacraft/ch02.htm . Go read that, then come
back here. I'll wait...

Now: Yes there is another way. Some (including myself) would argue that its is
in fact the proper way to do it. What you do is prepend the package name in
front of the name of every object in that package that you reference. For
instance, in examine.adb, "Spell" would become "Setup.Spell". The second thing
you will need to do is change that "use" clause to a "use type" clause for the
type in question (Setup.Method_Enum_Type). That gives you direct visibility to
built-in infix operators like "=".

I highly suggest all newbies use this approach rather than just doing a "use"
clause for the whole package, as it trains you to think about where you are
getting things from. One you become proficient with Ada, you can decide for
yourself if you need to continue working that way.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html

No trees were killed in the sending of this message. 
However a large number of electrons were terribly inconvenienced.



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

* Re: Problem with Enumaration and visibility
@ 2001-11-14 15:37 Hambut Frumblefoot
  0 siblings, 0 replies; 7+ messages in thread
From: Hambut Frumblefoot @ 2001-11-14 15:37 UTC (permalink / raw)
  To: comp.lang.ada

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=us-ascii, Size: 931 bytes --]

<snip>

-- if I add �use Setup;� in the Start procedure it 
-- works. Why do I have to do this? Is there a 
-- different way without using use. I�m just asking to

-  understand how this works with enumarated types

This error is to do with the visibility of 'free'
functions (i.e. functions that are declared implicitly
on definition of a type), e.g. '=', '/=' etc.  These
functions have the same visibility rules as other
sub-programs declared in a package.  These rule apply
regardless of the type of the type (if you see what I
mean).

Ways of avoiding 'use'*:
1.  'use type package.typename'
2.  function '=' renames examiner.'='

Hope this helps,

Hambut

* Don't try and type these in as they probably won't
work - I haven't checked the syntax.  Hopefully
they'll give a pointer.


__________________________________________________
Do You Yahoo!?
Find the one for you at Yahoo! Personals
http://personals.yahoo.com



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

* Re: Problem with Enumaration and visibility
  2001-11-14 13:03 ` Preben Randhol
  2001-11-14 13:48   ` Wilhelm Spickermann
  2001-11-14 15:28   ` Ted Dennison
@ 2001-11-14 16:32   ` Jeffrey Carter
  2001-11-14 17:25     ` Preben Randhol
  2 siblings, 1 reply; 7+ messages in thread
From: Jeffrey Carter @ 2001-11-14 16:32 UTC (permalink / raw)


Preben Randhol wrote:
> 
> package Setup is
> -- ...
> 
> type Method_Enum_Type is (Spell, MChoice);
> 
> procedure Get_Method return Method_Enum_Type;
> -- ...
> end Setup;
> 
> with Setup;
> 
> package body Examine is
> 
> -- ...
> 
>    procedure Start is
>       Method : Setup.Method_Enum_Type := Setup.Get_Method;
>    begin
>       if Method = Setup.Method_Enum_Type'(Spell) then
>          Examine.Spelling.Start;
>       end if;
>    end Start;

This has nothing specifically to do with enumeration types. Anything
declared in a package specification is not directly visible outside the
package.

In the fragment of Setup shown, you declare a type, 2 enumeration
literals (which are actually functions), a function (I presume, though
you call it a procedure). The type declaration implicitly declares a
number of operations, including "=", "/=", "<", ">", and so on. None of
these things are directly visible outside the package without a use
clause.

In Examine, you reference the type (Method_Enum_Type), an operation
("="), and an enumeration literal (Spell). Without a use clause, none of
these are directly visible; they must all be prefixed with "Setup.". You
did this for the type, but not for the operation or literal.

You have a number of choices:

1. Prefix everything:

if Setup."=" (Method, Setup.Spell) then

2. Add "use Setup;". This eliminates the need to prefix anything.

3. Add "use type Setup.Method_Enum_Type;". This eliminates the need to
prefix "=", but you will still need to prefix Spell.

4. Rename the operations of interest:

function "=" (Left, Right : Setup.Method_Enum_Type) return Boolean
renames Setup."=";
function Spell return Setup.Method_Enum_Type renames Setup.Spell;

if Method = Spell then

5. Some combination of the above.

This situation arises for anything declared in one package but
referenced elsewhere.

-- 
Jeffrey Carter



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

* Re: Problem with Enumaration and visibility
  2001-11-14 13:48   ` Wilhelm Spickermann
@ 2001-11-14 16:55     ` Preben Randhol
  0 siblings, 0 replies; 7+ messages in thread
From: Preben Randhol @ 2001-11-14 16:55 UTC (permalink / raw)


On Wed, 14 Nov 2001 14:48:47 +0100, Wilhelm Spickermann wrote:
> 
> 
> --On Mittwoch, November 14, 2001 13:03:19 +0000 Preben Randhol 
><randhol+abuse@pvv.org> wrote:
> 
> ...
>>       if Method = Setup.Method_Enum_Type'(Spell) then
>                   ^                         ^^^^^
> 
> These are two items which are defined in Setup and need a 
> "Setup." too, if you want to avoid using "use". This leads to:
> 
> ...
> if Setup."=" (Method, Setup.Method_Enum_Type'(Setup.Spell)) then
> ...
> 
> which can be simplified to:
> 
> ...
> if Setup."=" (Method, Setup.Spell) then
> ...
> 
> The ugly �Setup."="� can be avoided by a "use type" clause:
> 
> ...
>       use type Setup.Method_Enum_Type;
>       Method : Setup.Method_Enum_Type := Setup.Get_Method;
>    begin
>       if Method = Setup.Spell then


Thanks. I understand!

Preben



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

* Re: Problem with Enumaration and visibility
  2001-11-14 16:32   ` Jeffrey Carter
@ 2001-11-14 17:25     ` Preben Randhol
  0 siblings, 0 replies; 7+ messages in thread
From: Preben Randhol @ 2001-11-14 17:25 UTC (permalink / raw)


On Wed, 14 Nov 2001 16:32:58 GMT, Jeffrey Carter wrote:

> In the fragment of Setup shown, you declare a type, 2 enumeration
> literals (which are actually functions), a function (I presume, though
> you call it a procedure). The type declaration implicitly declares a

cut'n'paste error it is a function :-)

> You have a number of choices:

[..]

> 2. Add "use Setup;". This eliminates the need to prefix anything.

Don't like this for my packages.
> 
> 3. Add "use type Setup.Method_Enum_Type;". This eliminates the need to
> prefix "=", but you will still need to prefix Spell.

Yes this is nice.

Preben



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

end of thread, other threads:[~2001-11-14 17:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-14 15:37 Problem with Enumaration and visibility Hambut Frumblefoot
     [not found] <slrn9v4uqg.h34.randhol+abuse@kiuk0156.chembio.ntnu.n o>
2001-11-14 13:03 ` Preben Randhol
2001-11-14 13:48   ` Wilhelm Spickermann
2001-11-14 16:55     ` Preben Randhol
2001-11-14 15:28   ` Ted Dennison
2001-11-14 16:32   ` Jeffrey Carter
2001-11-14 17:25     ` Preben Randhol

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