Symbols in context qualified functions not in that context

Started by itistoday, October 26, 2009, 04:46:30 PM

Previous topic - Next topic

itistoday

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 http://newlispfanclub.alh.net/forum/viewtopic.php?f=8&t=2957">define-subclass macro I wrote:


(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))
   )
)


Although that lets you write really succinct and beautiful code like this:



(define-subclass (Bar Foo)
   ((get x) (x 2))
   ((set x v) (setf (x 2) v) x)
   ((str x) (string x))
)


It is currently useless for several reasons:



[*] 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
  • [/list]


    As another example, in working on the Dragonfly web framework for newLISP I thought it would be great if Dragonfly provided a convenient macro for creating routes like this](define-route (MyRoute)
       ((matches?)
          ... code ...
       )
       ((run)
          ... more code ...
       )
    )[/code]

    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:


    (context MAIN)
    (new Route 'Route.Resource)
    (context Route.Resource)

    (define (matches?)
    ... code ...
    )
    (define (run)
    ... more code ...
    )


    And that must be done for *each route*! Compare the two approaches:


    (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 ...
    )


    Versus:


    (define-route (Route.Resource)
    ((matches?)
    ... code ...
    )
    ((run)
    ... more code ...
    )
    )

    (define-route (Route.Static)
    ((matches?)
    ... code ...
    )
    ((run)
    ... more code ...
    )
    )


    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:


    (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)
    )


    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!
    Get your Objective newLISP groove on.

    m35

    I hate to say it, but the limitations surrounding context referencing are the longest running issues that have bugged me about newLISP. I first ran into them http://newlispfanclub.alh.net/forum/viewtopic.php?f=5&t=1643">over 2 years ago, shortly after stumbling upon the language.



    I really like newLISP, and definitely don't want to diminish Lutz's hard work and immeasurable generosity. I'm just bummed these limitations are still around.

    Lutz

    previous post here was in the wrong location and has been moved here:



    http://newlispfanclub.alh.net/forum/viewtopic.php?f=8&p=16978#p16978">http://newlispfanclub.alh.net/forum/vie ... 978#p16978">http://newlispfanclub.alh.net/forum/viewtopic.php?f=8&p=16978#p16978

    itistoday

    Quote from: "Lutz"
    QuoteReally? I thought "$this" referencing the function is impossible, because of "one reference only."


    ..[snip]..


    Wrong http://newlispfanclub.alh.net/forum/viewtopic.php?f=8&t=2961">thread?
    Get your Objective newLISP groove on.