Re: Why doesn't this work?

new topic     » topic index » view thread      » older message » newer message

----- Original Message -----
From: "Juergen Luethje" <j.lue at gmx.de>
To: "EUforum" <EUforum at topica.com>
Subject: Re: Why doesn't this work?


>
>
> Hello Jim, you wrote:
>
> > It fails, simply because, inc_2.e has no show() routine defined.
> >
> > inc_1.e does, and inc_a.e does, but neither of them are in the "two"
> > namespace.
>
> But the show() routine in 'inc_a.e' is global, 'inc_a.e' is included in
> 'inc_2.e', and 'inc_2.e' has the "two" namespace assigned.

What Jim was meaning was that inc_2.e does not CALL the routine named
'show()', rather than is doesn't contain the routine. Namespace clashes ONLY
occur when duplicated names are referred to. Simply including them doesn't
cause an error or warning message.


Your code ...
 ------=-------------------------=------
 -- main.exw
 include inc_1.e as one
 include inc_2.e as two

 one:show() -- means call the 'show' routine DEFINED in inc_1.e
 two:show() -- means call the 'show' routine DEFINED in inc_2.e

however if inc_2.e looks like this...

 ------=-------------------------=------
 -- inc_2.e
 include inc_a.e
 ------=-------------------------=------

you can see that there is NO 'show' routine defined in this file. The 'show'
routine is actually defined in inc_a.e. Thus if that's the one you really
want to call, you need to write in you main program this ...


 -- main.exw
 include inc_1.e as one
 include inc_2.e
 include inc_a.e as two -- Yes, even though it is already included in
inc_2.e !!

 one:show() -- means call the 'show' routine DEFINED in inc_1.e
 two:show() -- means call the 'show' routine DEFINED in inc_a.e

> Does that mean, that the namespace of a global symbol has not the same
> <looking for a proper english word...> size/reaching distance/range than
> it's scope? (Is this understandable?) That confuses me. I also didn't
> find an explanation in the docs.

Namespaces are local. They only last as long as the file they are defined
in. You cannot 'see' the namespace that was defined inside another file.

> > I feel this is correct behavior (if you want to access inc_a.e's routine
> > directly, include it directly in main.exw).
>
> But without an explicite namespace, 'inc_a.e's routine can also be
> accessed, if 'inc_a.e' is included indirectly in 'main.exw' via
> 'inc_2.e'.

Realize that just including a file does NOT cause namespace clashes - you
must also refer to the duplicated global name to cause an error.

> (I wrote another, for me even more surprising example at the end of this
> post.)
>
> > jbrown
> >
> > On Mon, May 19, 2003 at 06:57:45PM +0200, Juergen Luethje wrote:
> >>
> >> Hello Rob, you wrote:
> >>
> >> <big snip>
> >>
> >>> Whenever a file is included, the global symbols in that file
> >>> become visible in the including file, and all the code
> >>> that comes later (in other files).
> >>
> >> It seems to me, that regarding to namespaces, one aspect of this hasn't
> >> been discussed yet (unless I missed it).
>
> <snip>
>
> >> ------=-------------------------=------
> >> -- main.exw
> >> include inc_1.e as one
> >> include inc_2.e as two
> >>
> >> one:show()
> >> two:show()
> >> ------=-------------------------=------
> >>
> >> ------=-------------------------=------
> >> -- inc_1.e
> >> global procedure show ()
> >>    puts(1, "printing from 'inc_1.e'\n")
> >> end procedure
> >> ------=-------------------------=------
>
> <snip>
>
> >> ------=-------------------------=------
> >> -- inc_2.e
> >> include inc_a.e
> >> ------=-------------------------=------
>
> <snip>
>
> >> ------=-------------------------=------
> >> -- inc_a.e
> >> global procedure show ()
> >>    puts(1, "printing from 'inc_a.e'\n")
> >> end procedure
> >> ------=-------------------------=------
> >>
> >>
> >> Now running 'main.exw', I'd expect to see:
> >>    printing from 'inc_1.e'
> >>    printing from 'inc_a.e'
> >>
> >> But Euphoria (interpreter 2.4 Beta) says:
> >>    printing from 'inc_1.e'
> >>    D:\test\main.exw:6
> >>    show has not been declared
> >>    two:show()
> >>           ^
>
> <snip>
>
>
> ---------=--------------[ New example ]---------------=---------
>
> ------=-------------------------=------
> -- inc_a.e
> global procedure show ()
>    puts(1, "printing from 'inc_a.e'\n")
> end procedure
> ------=-------------------------=------
>
> ------=-------------------------=------
> -- inc_2.e
> include inc_a.e
> ------=-------------------------=------
>
> ------=-------------------------=------
> -- main.exw
> include inc_2.e
>
> show()
> ------=-------------------------=------

Of course it does. Because there is only ONE global symbol called 'show'.

>
> The code above works fine. Now I just add an explicite namespace in
> 'main.exw', which is a legal operation, and it doesn't work anymore
> (all the time talking about v2.4 Beta, of course):
>
> ------=-------------------------=------
> -- main.exw
> include inc_2.e as two
>
> two:show()

It doesn't work because the line "two:show()" means "call the routine named
'show' that has been DEFINED in the namespace 'TWO'". And there isn't one in
this example!

> ------=-------------------------=------
>
> This confuses me, and is IMHO in contrast to simplicity, one of
> Euphoria's great advantages.
>

Most things are simple once you know the rules. And the rule when using
namespace qualifiers is that the thing you are referring to must be DEFINED
in the namespace - not just indirectly included.

----------------
cheers,
Derek Parnell

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu