Page 1 of 1
[BUG] save non valid symbols (context)
Posted: Sun Aug 20, 2006 9:21 pm
by starseed
Hi Lutz,
I created some symbols using sym, with strings which are really far from being valid symbols.
Code: Select all
(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:
Code: Select all
(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]
Posted: Sun Aug 20, 2006 9:44 pm
by Lutz
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
Posted: Mon Aug 21, 2006 7:27 am
by starseed
Thanks,
I just wasn't sure, wether it's a known problem.
Ingo
Posted: Mon Aug 21, 2006 1:29 pm
by Lutz
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':
Code: Select all
(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
Posted: Mon Aug 21, 2006 3:02 pm
by starseed
Lutz wrote: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.
Lutz wrote: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.
Lutz wrote: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':
Code: Select all
(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
Posted: Mon Aug 21, 2006 3:30 pm
by Lutz
I'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
Posted: Mon Aug 21, 2006 5:12 pm
by starseed
Thats something I'm just now thinking about ...
I like to have named elements in datasets. Do have a better idea than the following?
Code: Select all
(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
Posted: Tue Aug 22, 2006 3:55 pm
by Lutz
Yes, this is the right way to do it, typical application for 'assoc'/'lookup'
Lutz