Hash functions within a context do not see context members

Q&A's, tips, howto's
Locked
Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Hash functions within a context do not see context members

Post by Tim Johnson »

I have a solution, but don't fully understand the issue:
I've set up a typehandler/callback using a hash with functions as in

Code: Select all

(define typeHandler:typeHandler)
(typeHandler "text"
    (fn (fl val ndxs DS DSndx)
        (letn((ndx (ndxs 0))(ele (fl ndx))
                (attrs(set-attr (list "value" val)(ele 2 1))))
         (setf (DS DSndx ndx 2 1) attrs)
         DS)))
Even though the code above is lexically within a context, the functions defined as members of
the hash do not recognize members of the outer context.
To elaborate, I have to pass the context member DS as the fourth argument to the
(typeHandler "test") lambda and the context member current-DS-index (DSndx)
as the fifth argument.
I can live with that, but I'd welcome comments or whether or not I may be missing
something.
thanks
tim
Programmer since 1987. Unix environment.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Hash functions within a context do not see context members

Post by Lutz »

No sure what you mean. In the following smaller example you can see 'outer-var' recognized:

Code: Select all

(define typeHandler:typeHandler)

(set 'outer-var 999)

(typeHandler "text" (fn (x y z) (println x  ":" y ":" z ":" outer-var)))

((typeHandler "text") 1 2 3)

1:2:3:999
and the following with the code inside a context FOO will also work:

Code: Select all

(define typeHandler:typeHandler)

(context 'FOO)

(set 'outer-var 999)

(typeHandler "text" (fn (x y z) (println x  ":" y ":" z ":" outer-var)))

(context MAIN)

((typeHandler "text") 1 2 3)

1:2:3:999

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Hash functions within a context do not see context members

Post by Tim Johnson »

Lutz wrote:No sure what you mean. In the following smaller example you can see 'outer-var' recognized:
Note that I placed the define for 'typeHander inside of the context.
I moved the define outside of the context and now the hash functions have context scope. I.E. It appears
that the context members are now visible to the type handler functions.
Thanks for the example. That showed me what to do.

cheers
tim
Programmer since 1987. Unix environment.

Locked