[BUG] save non valid symbols (context)

Started by starseed, August 20, 2006, 02:21:47 PM

Previous topic - Next topic

starseed

Hi Lutz,



I created some symbols using sym, with strings which are really far from being valid symbols.


(context 'block)
(set 'my-list nil)
(set 'my-index nil)

(define (block:block)
   (my-list my-index))

(define (block:new alist)
   (let ((new-block (MAIN:new MAIN:block (gensym 'MAIN))))
      (new-block:init alist)
      new-block))

(set 'a (block:new))


With gensym creating symbols like:  _(-(_gensym_0.1_)-)_

And just for the fun of it, I tried to save this mess ...



This is what I found in the file:


(context '_(-(_gensym_0.1_)-)_)

(set (sym "_(-(_gensym_0.1_)-)_" _(-(_gensym_0.1_)-)_)
 (lambda () (my-list my-index)))


Of course, this won't load ...





Kind regards,



Ingo[/code]

Lutz

#1
Currently it does not work for symbols for context/namespace but it works correctly for other symbols representing each symbol with a (sym "......") statement, which is what you see.



Context symbols have to be legal symbols. If you leave the context symbols legal, you should be fine for other symbols. You can test legality of a symbol using the built-in function 'legal?'.



Lutz

starseed

#2
Thanks,



I just wasn't sure, wether it's a known problem.





Ingo

Lutz

#3
Few would use non legal symbols to name a context/namespace, but illegal symbols may occur in dictionaries from life non-program texts and then the contsruction (sym "....." aContext) is useful. There are no plans to allow this for the symbols naming contexts.



But here is another trick (undocumented) , which may be useful in your case. Put [ and ] as the first and last character of your symbols name. When the newLISP scanner finds a [ it will eat any other character until a closing ] and it can work as a symbol. The following snipped was generated on the keyboard saved with 'save' and reloaded with 'load':



(context '[&9*^(& ()$&^%])

(set  (sym "[jh((g90990&]" [&9*^(& ()$&^%])  999)

(context 'MAIN)


In the 'set' statement you see that the context name works as a symbol, although it contains illegal characters.



Lutz

starseed

#4
Quote from: "Lutz"Few would use non legal symbols to name a context/namespace,


That's exactly why I used it ;-) I wanted to be sure to not clash with anything else. But it's not a big deal, I can safely use legal symbols.


Quote from: "Lutz"but illegal symbols may occur in dictionaries from life non-program texts and then the contsruction (sym "....." aContext) is useful. There are no plans to allow this for the symbols naming contexts.


I'm somewhat accustomed to using lightweight objects (1). So I tried to implement something, I felt at home with. I'm not yet familiar enough to do everything "the newLisp way", it seems ...



(1)Actually, Rebol is somewhat orthogonal to newLisp: Rebol programmes use objects to implement namespaces, whereas in newLisp you can nuse namespaces to implement objects.




Quote from: "Lutz"But here is another trick (undocumented) , which may be useful in your case. Put [ and ] as the first and last character of your symbols name. When the newLISP scanner finds a [ it will eat any other character until a closing ] and it can work as a symbol. The following snipped was generated on the keyboard saved with 'save' and reloaded with 'load':



(context '[&9*^(& ()$&^%])

(set  (sym "[jh((g90990&]" [&9*^(& ()$&^%])  999)

(context 'MAIN)


In the 'set' statement you see that the context name works as a symbol, although it contains illegal characters.



Lutz


Thank, that may come in handy at times



Ingo

Lutz

#5
QuoteI'm somewhat accustomed to using lightweight objects


That would be ok in newLISP too, as long as your objects have a longer life and are not too volatile.



In newLISP namespace objects are bound to a name, and allthough creating symbols is fast, and modifying their contents is fast too, deleting the symbol from the symbols table takes more time.



For that reason objects in newLISP tend to have a longer life, like code modules, longer living data objects etc. If your data objects are destroyed frequently, namespaces are not the right form to represent them and you are better of using lists, lists of lists etc.



newLISP is primarily a language defining objects in terms of lists, wich are created and deleted much more efficiently.



Lutz

starseed

#6
Thats something I'm just now thinking about ...



I like to have named elements in datasets. Do have a better idea than the following?


(set 'persons '( ((name "Ingo") (last "Hohmann") (birth-place {Planet Earth})) ((name "ET") (birth-place "Sirius III"))))

(filter  (lambda (v) (= "Ingo" (lookup 'name v 1))) persons)




Thanks,



Ingo

Lutz

#7
Yes, this is the right way to do it, typical application for 'assoc'/'lookup'



Lutz