Perhaps modules should be valid containers. Thus instead of introducing a new syntax foo@a, we can use (foo 'a) and foo!a using sugar already in place. (I realize I am not the first person to say that.) I also would not like having to use mdef and m= within a module. Ideally, I should just use def, mac, and = as normal and export the symbols I want visible from outside.
(module foo (export a bar)
(= a 'in-top)
(def foo (x) (+ x 1))
(def bar (x) (+ x 1))
(pr a))
(prn foo!a)
(foo!bar 0)
(= bar foo!bar) ; an import.
(= a2 foo!a) ; a qualified import.
This could all be implemented as an optional library, except for those pesky macros. And of course, we do need a module system with adequate macro support.
What about something like what you are proposing, but where you have to explicitly state when you use the module namespace, since we obviously can't overwrite = without sad effects ?
For example, let's take the $ symbol (another ugly one :) to mean "the current module". The above could be written :
(module foo (export a bar)
(= $!a 'in-top)
(def $!foo (x) (+ x 1))
(def $!bar (x) (+ x 1))
(pr a))
(prn foo!a)
(foo!bar 0)
(= bar foo!bar) ; an import.
(= a2 foo!a) ; a qualified import.
Advantages :
- very simple
- written in pure Arc (thus candidate to the core language)
Here's a macro implementing part of that behavior :
(= $ nil)
(= $path* '()) ; Current module hierarchy
(mac module (name . body)
(w/uniq old-$
`(with (,old-$ $)
(= $ (table))
(push $ $path*)
,@body
(pop $path*)
(if $path*
(= (,old-$ ',name) $) ; Put it in the parent
(= ,name $)) ; Put it in global namespace
(= $ ,old-$))))
It supports imbricated modules. To import a module, or do a qualified import, the classical table manipulation functions work.
Question (dunno the answer, not on my hacking computer): how will a macro receive the expression (foo:bar foo)? As (foo (bar foo))? as ((compose foo bar) foo)? As (|foo:bar| foo)? (|| is used to enclose a literal in symbols)
There's a problem with using = and def. I tried it, but many parts of the core and libraries assume a unique namespace. As commands can have many side-effects, everything breaks easily.
Here's a different idea. Suppose that instead we build a basic modulesystem which transforms:
(modules-base
;name of module.
foo
;set of functions in this module
(bar)
;set of module variables
(nitz)
;set of functions from other modules
((module2 hmm niawniaw))
(def bar (x) (hmm) (niawniaw) (do1 nitz (= nitz x))))
(modules-base
foo
(bar)
(nitz)
;gotten by taking (keys module2)
((module2 hmm niawniaw))
(def bar (x) (do1 nitz (= nitz x))))
Weaknesses: (1) we can't make module-variables accessible outside. If we had access to environments, though, we could.
(2) macros are impossible as yet, whether shared or not. Possibly, we need macrolet, and adding some mechanism to store macros separately from the module table - possibly in a table-of-tables module-macros.
Implementation: simple scanning would be nice. However, modules-basic would be better implemented by a 'macrolet form.