Implicit creation of contexts not working newlisp 10.5.3

Q&A's, tips, howto's
Locked
bharath_g
Posts: 1
Joined: Sun Sep 08, 2013 6:50 am

Implicit creation of contexts not working newlisp 10.5.3

Post by bharath_g »

In the newlisp manual in the "Creating contexts" section it is mentioned that contexts are created
implicitly when referring to a context that does not exist.
This is working in general but if i use a single character as a context name then it fails with the following error
If i try to do this

Code: Select all

(define (D:foo x y)
  (+ x y))
It fails with error
ERR: context expected in function define : D
It works ok if i just use a 2 character name for the context like this

Code: Select all

(define (DC:foo x y)
  (+ x y))

JohnE
Posts: 14
Joined: Thu Nov 01, 2007 5:08 pm

Re: Implicit creation of contexts not working newlisp 10.5.3

Post by JohnE »

Hello,

I've just tried it:

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

> (define (D:foo x y) (+ x y))
(lambda (x y) (+ x y))
> (D:foo 3 5)
8
>
-------------------------------

However, if D is already defined as something else, it fails. I think that's reasonable.
This is what I did after the above test:

------------------
> (delete 'D)
true
> (D:foo 3 5) <----------- check if it's gone

ERR: context expected : D <----------- Yes, it's gone
>
>
> (set 'D 0)
0
> D
0
> (define (D:foo x y) (+ x y))

ERR: context expected in function define : D <----------- D already exists as something else
>
---------------------------------


John

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

Re: Implicit creation of contexts not working newlisp 10.5.3

Post by Lutz »

That is correct, it failed for bharath_g, because the variable already existed. But you can convert an already existing variable to a context using the context function:

Code: Select all

> (set 'D 123)
123
> (define D:foo 456)

ERR: context expected in function define : D
> (context 'D "foo" 456)     ;<-- but this will work
456
> D:foo
456
>
it's only the implicit creation which is not allowed on existing variables. It also works for functions:

Code: Select all

> (set 'D 123)
123
> (context 'D "foo" (lambda (x y) (+ x y)))
(lambda (x y) (+ x y))
> (D:foo 3 4)
7
> 

Locked