Namespaces

15 views
Skip to first unread message

Humberto Anjos

unread,
Jan 6, 2012, 6:23:17 PM1/6/12
to magpi...@googlegroups.com
Does Magpie have namespaces, or something like it? Are there any plans?

Bob Nystrom

unread,
Jan 6, 2012, 11:46:23 PM1/6/12
to magpi...@googlegroups.com
On Fri, Jan 6, 2012 at 3:23 PM, Humberto Anjos <h.a...@gmail.com> wrote:
> Does Magpie have namespaces, or something like it? Are there any plans?

Every module (source file) in Magpie has its own distinct top-level
scope. There's no monolithic global scope.

When you import a module, by default it will import names directly
into the importing module's top-level scope, but you can choose to
prefix things on import, rename, exclude some names, etc.

- bob

Humberto S. N. dos Anjos

unread,
Jan 7, 2012, 6:43:34 AM1/7/12
to magpi...@googlegroups.com
I was going to write about every import having to be automatically qualified for sanity reasons, but having no shared global scope does help to avoid spooky action at a distance, so never mind :)  Except for shared multimethods, as far as I can see. Are shared multimethods figured out at load time?

And is there some notion of packages? In the source files as distributed below:

src/
outer.mag

inner/
innards.mag

How would outer.mag import innards.mag?

Bob Nystrom

unread,
Jan 7, 2012, 2:01:45 PM1/7/12
to magpi...@googlegroups.com
On Sat, Jan 7, 2012 at 3:43 AM, Humberto S. N. dos Anjos
<h.a...@gmail.com> wrote:
> I was going to write about every import having to be automatically qualified
> for sanity reasons, but having no shared global scope does help to avoid
> spooky action at a distance, so never mind :)

Yeah, it shouldn't be too bad the way it's designed. Assuming I
designed it right. :)

> Except for shared
> multimethods, as far as I can see. Are shared multimethods figured out at
> load time?

"Load time" isn't really well-defined in Magpie right now. The first
time you import a module, it executes it. Top-level def expressions
will be evaluated which will add new specializations to multimethods.
If that multimethod happens to have been imported from another module,
that module will see it too. As long as this happens at the top level
before your main script starts doing real work, everything should be
all wired up before you start calling things.

I'm considering changing the grammar to be a little more declarative
at the top level so a script only contains imports and definitions at
the top and imperative code lives inside methods. (In other words,
less like Python/Ruby/JS and more like C/C#/Java/Dart.)

If I do that, then multimethods would be resolved "more statically", I think.

>
> And is there some notion of packages? In the source files as distributed
> below:
>
> src/
> outer.mag
> inner/
> innards.mag
>
> How would outer.mag import innards.mag?

The syntax is still in flux a bit, but I believe it would be:

import .inner.innards

The leading dot means it's a relative import and then subsequent dots
are turned into path separators. I'm still fuzzy on whether relative
imports should be relative to the importing module's parent directory,
or itself (in the case where you have both foo.mag and a matching foo/
directory containing submodules of foo).

It's definitely a use case that I intend for Magpie to support, but it
might not work exactly right now. :)

- bob

Reply all
Reply to author
Forward
0 new messages