Not Switching Contexts and a Constant Question

Q&A's, tips, howto's
Locked
m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Not Switching Contexts and a Constant Question

Post by m i c h a e l »

I noticed while using net-eval that context doesn't actually switch the context:

Code: Select all

> (net-eval "localhost" 4711 "(context 'C)" 1000)
C
> (net-eval "localhost" 4711 "(context)" 1000)
MAIN
> _
This is similar to what happens when using eval in the following way:

Code: Select all

> (dolist (ea '((context 'C) (constant 'v 8) (context MAIN))) (eval ea))

ERR: symbol not in current context in function constant : MAIN:v
> _
Also, shouldn't constant work like set in the following example?

Code: Select all

> (new Class 'Thing)
Thing
> (set 'Thing:attribute 1)
1
> (constant 'Thing:attribute 1)

ERR: symbol not in current context in function constant : Thing:it
> _
If you want to use constants for named indexes (a good idea), then you're forced to define your class within a context. This is a must for large, complex classes, but for simple classes, I like the minimalism of defining within the MAIN context.

m i c h a e l

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

Re: Not Switching Contexts and a Constant Question

Post by Lutz »

- first example -

newLISP server internally does a reset, stack cleanup and reinitialization of all signals after each client connection. If running a stateful server only the contents of variables is kept.

But it will switch, you can see when packing both expressions in one transaction:

Code: Select all

> (net-eval "localhost" 4711 "(begin (context 'C) (context))")
C
 
> (net-eval "localhost" 4711 "(context? C)")
true
You also can see, it kept the context.

- second and third example -

You have to be inside the context to make symbols constant. This is documented in the reference for 'constant'.

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: Not Switching Contexts and a Constant Question

Post by m i c h a e l »

Thanks, Lutz. This topic came up while working on the new FOOP video. Or more specifically, the new line-commander (the code that simulates typing on the command-line). In line-commander, I evaluate each line one-by-one to get the result we see. I noticed that context didn't switch contexts when doing it this way (the second example). In order to cope with this, I began "faking it" and evaluating all expressions in MAIN by fully qualifying the symbols. Unfortunately, the problem with using constant for named indexes popped up as a result. At this point, I'm just defining them with setq.

m i c h a e l

Locked