Bug of (context Foo)?

Q&A's, tips, howto's
Locked
limux
Posts: 6
Joined: Sat Aug 17, 2013 1:59 am

Bug of (context Foo)?

Post by limux »

the code

Code: Select all

(set 'Foo:name "limux")
(context Foo)
is ok.

but

Code: Select all

(context Foo)
(set 'Foo:name "limux")
is bad.

I think the bug is if you run the "(context Foo)" first, then "(set 'Foo:name "limux")"
will always report error.

Great thanks for more details about context or demo code!

conan
Posts: 52
Joined: Sat Oct 22, 2011 12:14 pm

Re: Bug of (context Foo)?

Post by conan »

You forgot to quote Foo on your second example:

Code: Select all

(context 'Foo)
Other than that I don't see anything wrong with that. What error are you seeing?

limux
Posts: 6
Joined: Sat Aug 17, 2013 1:59 am

Re: Bug of (context Foo)?

Post by limux »

I am not forgot the quote, I just do that.

Because (context Foo) is the first code line. It's surely failed.
The question is once that code be evaluted, I wonder why the (set 'Foo:name nil) also will be failed.
but in a new clean newlisp instance, the (set 'Foo:name nil) will be ok without evaluated (context Foo).

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

Re: Bug of (context Foo)?

Post by Lutz »

The evaluated argument of context has to be either a symbol or a context.

If Foo is not a context yet, Foo evaluates to nil and nil is not a context. To make a context out of Foo it must be given quoted as a symbol.

Code: Select all

newLISP v.10.5.4 32-bit on Win32 IPv4/6 libffi, options: newlisp -h

> (context? Foo)    
nil
> (context Foo)

ERR: symbol or context expected in function context : Foo
> (context 'Foo)
Foo
Foo> (context MAIN)
MAIN
> Foo  ; variable Foo evaluates to context Foo
Foo
> (context? Foo)  ; no quote necessary because variable Foo contains context Foo
true
> (context Foo)  ; switches to existing context Foo
Foo
Foo> (context MAIN)
MAIN
> (set 'ctx Foo)  ; the variable ctx contains a context
Foo
> (context ctx)
Foo
Foo> (context MAIN)
MAIN
> (set 'Bar:var 123)  ; context Bar with symbol var in it gets created
123
> (context? Bar)
true
> (context Bar)
Bar
Bar> (context MAIN)
MAIN
>

limux
Posts: 6
Joined: Sat Aug 17, 2013 1:59 am

Re: Bug of (context Foo)?

Post by limux »

Please see detailed the following code and is there any issue?

Code: Select all

> (set 'Foo1:var1 nil)
nil
> (context? Foo1)
true
> (context Foo2)

ERR: symbol or context expected in function context : Foo2
> (set 'Foo2:var2 nil)

ERR: context expected in function set : Foo2
> (context? Foo2)
nil
> (set 'Foo2:var2 nil)

ERR: context expected in function set : Foo2

> (context? Foo2)
nil

> 
Thanks for your reply. I think you are not understand my true ideas.

Before I define context Foo1, Foo1 is nil. (set 'Foo1:var1 nil) will surely define the Foo context,
then (context Foo1) is ok and (context2 Foo2) will return true.
See Foo2, It is also not defined, (context? Foo2) is nil and (context Foo2) will show errors. then
how can I define the Foo2 just after (context Foo2) which make some error?
(set 'Foo2:var2 nil) will always failed. This is my messes! If Foo2 has even not defined and you
evaluate (context Foo2) with some error, you will never define the context Foo2 by
"(set 'Foo2:var2 nil)". So I think it's a bug, perhaps!

I come from china, I love lisp but my english is a little bad, thanks your guys!

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

Re: Bug of (context Foo)?

Post by Lutz »

The implicit creation of a new context via the (set 'Foo:name ...) statement only works when the symbol hasn't been introduced before. Once the symbol exists, only the context function can be used to make a context out of it:

Code: Select all

newLISP v.10.5.4 32-bit on Win32 IPv4/6 libffi, options: newlisp -h

> Foo   ; Foo has now been "seen" and an ordinary symbol Foo is created
nil
> (set 'Foo:name 123) ; Foo cannot be converted to a context via set

ERR: context expected in function set : Foo
> (context 'Foo) ; only the context function allows conversion of ordinary vars into contexts
Foo
Foo> (context MAIN)
MAIN
>

> (set 'Bar:name 456) ; implicit creation of context Bar works, because it hasn't been seen before
456
>
This is a safeguard for not screwing up code entered before by accidental (set 'Foo:name ...) statements. All context creating statements like (set 'Foo:name ...) or better (define Foo:name ...) should be at the beginning of your program. The best is to create and introduce all contexts in the MAIN module.

If you wanted, you still could force (set 'Foo:name ...) to work by deleting the smbol before:

Code: Select all

newLISP v.10.5.4 32-bit on Win32 IPv4/6 libffi, options: newlisp -h

> Foo
nil
> (delete 'Foo)  ; symbol Foo is deleted again
true
> (set 'Foo:name 123)  ; now implicit context creation of Foo works
123

limux
Posts: 6
Joined: Sat Aug 17, 2013 1:59 am

Re: Bug of (context Foo)?

Post by limux »

I see, great thanks!

Foo introduced the "Foo" as a symbol with value of nil.
and (context Foo) also implicit introduce the symbol "Foo", although throw a error
"ERR: symbol or context expected in function context : Foo",

Is it right?

I should to read the source of newLisp I think.

limux
Posts: 6
Joined: Sat Aug 17, 2013 1:59 am

Re: Bug of (context Foo)?

Post by limux »

even (symbol? Foo) will implicit introduce a symbol. That is newLisp style!

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

Re: Bug of (context Foo)?

Post by Lutz »

(symbol? Foo) would check the contents of Foo, not the symbol Foo itself. But yes, the moment any code is loaded, symbols are created the first time they occur.

limux
Posts: 6
Joined: Sat Aug 17, 2013 1:59 am

Re: Bug of (context Foo)?

Post by limux »

Very thanks and Best Regards.

Locked