Return a context from a function

Started by Tim Johnson, May 09, 2008, 06:17:48 PM

Previous topic - Next topic

Tim Johnson

I'd like to have a function that creates and returns a new context from an existing source context:
(context 'hash)
(setq context-name "hash"    ## name of context
 object-name  "[none]"  ## name of object
 lst '()                ## list - may be nested
 )
(context 'MAIN)
(define (Hash! l oname)
  (local (newhash)
(set 'newhash(new hash 'newhash))
(println "newhash = ["  newhash:lst "]") ;; DEBUG newhash
(if l (set 'newhash:lst l))
(println "newhash = ["  newhash:lst "]") ;; DEBUG newhash
(if oname (set 'newhash:object-name oname))
(println "newhash = ["  newhash:object-name "]") ;; DEBUG newhash
newhash))

I evaluate the following with result:
> (set 'y (Hash! '((name "tim")) "myhash"))
newhash = [()]
newhash = [((name "tim"))]
newhash = [myhash]
newhash

And then evaluate 'y with (obviously) not the results I'm looking for.

> y:lst
?
> y:object-name
?

Help and examples are welcome. I haven't been able to find any

examples of this specific topic in documentation.

Thanks

Tim
Programmer since 1987. Unix environment.

Lutz

#1
Not sure I understand what you want to do, but making a context from a source context is just one statement using 'new':


(context 'Source)
(set 'varA 123)
(set 'varB "hello")
(context MAIN)

(new Source 'Target)

; now Target is a copy of Source

> Target:varA
123
> Target:varB
"hello"
>


If you want to put this into a function, you would do:


(define (copy from to)
(new from to))

; now use the new function

> (set 'y (new Source 'Target))
Target
> y:varA
123
> y:varB
"hello"

; y contains the context Target
> (symbols y)
(Target:varA Target:varB)
>


The problem in you code is having 'newhash' as a local variable, the contents of which will be deleted when exiting the 'local' statement. Don't put the name of a context as a local variable. This would imply a context beeing anonymous. A context always must be bound to a symbol in MAIN.

Tim Johnson

#2
Hi Lutz:

Since I sent the previous, I find that the following:
(define (Hash! l oname)
    (new hash 'newhash)
(println "newhash = ["  newhash:lst "]") ;; DEBUG newhash
(if l (set 'newhash:lst l))
(println "newhash = ["  newhash:lst "]") ;; DEBUG newhash
(if oname (set 'newhash:object-name oname))
(println "newhash = ["  newhash:object-name "]") ;; DEBUG newhash
newhash)

gives me results closer to what I'm looking for - at least with preliminary testing,

and the changes are consistent with your advice about not making

the variable local.

To clarify what I am after:

I'd like to be able to instantiate any number of "objects" which might be

multi-level associative lists. This is consistent with much that I now do in other languages

and consistent with RAD tools that I've developed for python and rebol

code generation.



Python has the feature of object intializers - so I'm trying to replicate that

here with the Hash! function. At a top level I might have something like this:

(set 'lst '(
    (id001 (name "Anne") (addr (country "USA") (city "New York")))
    (id002 (name "Jean") (addr (country "France") (city "Paris")))
))
(set 'records (Hash! lst "records"))
;; here we have a default function
(define (hash:hash)
  (let((res lst))
(dolist (i (args) (not res))
(set 'res (assoc i res)))))
;; and a function call like
(records 'id001 'addr 'country)

Such an instantiation might have a 'strict parameter that, if

set might result in an error thrown if for instance, a non-nil

search result - and such an error would

contain the name of the context ('context-name) and the name

of the instantiation ('object-name).

Are my intentions any clearer now? :-)

Thanks again

tim
Programmer since 1987. Unix environment.

Lutz

#3
We posted at the same time, I added to my previous post, while you where posting ;-)

Tim Johnson

#4
Quote from: "Lutz"We posted at the same time, I added to my previous post, while you where posting ;-)

"""

Great Minds Run in the Same Gutter -

Even in different time zones.

""" - Alaskus Curmudgeous
Programmer since 1987. Unix environment.

Lutz

#5
Have you also looked into this?



http://newlisp.org/download/development/newlisp_manual.html#hash">http://newlisp.org/download/development ... .html#hash">http://newlisp.org/download/development/newlisp_manual.html#hash



available in current development version 9.3.11

Tim Johnson

#6
Quote from: "Lutz"Have you also looked into this?



http://newlisp.org/download/development/newlisp_manual.html#hash">http://newlisp.org/download/development ... .html#hash">http://newlisp.org/download/development/newlisp_manual.html#hash



available in current development version 9.3.11

Very interesting!

Looking forward to it in the stable distro ....

thanks

tim
Programmer since 1987. Unix environment.