Symbols in context qualified functions not in that context
Posted: Mon Oct 26, 2009 11:46 pm
One area that I think newLISP could use a lot of improvement in is in dealing with contexts.
I've been thwarted multiple times in my attempts at creating useful and awesome macros for newLISP's FOOP stuff.
As just one example consider the define-subclass macro I wrote:
Although that lets you write really succinct and beautiful code like this:
It is currently useless for several reasons:
While this can be done, it's severely handicapped right now because of the previously stated reasons. It can't be called outside of the MAIN context, and any symbols in those 'matches?' and 'run' functions will be in the MAIN context (unless each is verbosely qualified), despite the current ability of creating such a macro that context-qualifies the 'matches?' and 'run' functions.
Instead, routes currently must be defined verbosely like this:
And that must be done for *each route*! Compare the two approaches:
Versus:
Why does this happen? Because newLISP can't switch contexts inside of a function call.
Thus, I'd like to be able to rewrite the 'define-subclass' fexpr like this, but I can't:
Any chance of this becoming possible? Or at least making 'eval' place unqualified symbols in the same context as the function? I think it would be a great boon the language if such things were possible in newLISP, the possibilities it would open would be great!
I've been thwarted multiple times in my attempts at creating useful and awesome macros for newLISP's FOOP stuff.
As just one example consider the define-subclass macro I wrote:
Code: Select all
(define-macro (define-subclass)
(new (args 0 1) (args 0 0))
(dolist (method (rest $args))
(setf (method 0 0) (sym $it (args 0 0)))
(eval (push 'define method))
)
)
Code: Select all
(define-subclass (Bar Foo)
((get x) (x 2))
((set x v) (setf (x 2) v) x)
((str x) (string x))
)
- It can't be called outside of the MAIN context.
- But most importantly, all of the variables and parameters inside those functions are in the MAIN context, not the Bar context
Code: Select all
(define-route (MyRoute)
((matches?)
... code ...
)
((run)
... more code ...
)
)
Instead, routes currently must be defined verbosely like this:
Code: Select all
(context MAIN)
(new Route 'Route.Resource)
(context Route.Resource)
(define (matches?)
... code ...
)
(define (run)
... more code ...
)
Code: Select all
(context MAIN)
(new Route 'Route.Resource)
(context Route.Resource)
(define (matches?)
... code ...
)
(define (run)
... more code ...
)
(context MAIN)
(new Route 'Route.Static)
(context Route.Static)
(define (matches?)
... code ...
)
(define (run)
... more code ...
)
Code: Select all
(define-route (Route.Resource)
((matches?)
... code ...
)
((run)
... more code ...
)
)
(define-route (Route.Static)
((matches?)
... code ...
)
((run)
... more code ...
)
)
Thus, I'd like to be able to rewrite the 'define-subclass' fexpr like this, but I can't:
Code: Select all
(define-macro (define-subclass)
(new (args 0 1) (args 0 0))
(context (args 0 0))
(dolist (method (rest $args))
(setf (method 0 0) (sym $it (args 0 0)))
(eval (push 'define method))
)
(context MAIN)
)