The sample code of Passing data by reference

Started by johu, September 15, 2010, 02:38:49 AM

Previous topic - Next topic

johu

Quote;; pass data by context reference



(set 'Mydb:data (sequence 1 100000))



(define (change-db obj idx value)

    (setf (obj:data idx) value))



(change-db Mydb 1234 "abcdefg")



(nth (Mydb:data 1234))  → "abcdefg"

According to Users Manual and Reference,


QuoteSymbol creation in contexts



3.When an unknown symbol is encountered during code translation, a search for its definition begins inside the current context. Failing that, the search continues inside MAIN for a built-in function, context, or global symbol. If no definition is found, the symbol is created locally inside the current context.

Therefore(?), obj:data cause the following error message:


ERR: context expected in function match : obj
called from user defined function $reader-event

I think some alternative code.


Quote(define (change-db obj idx value)

    (setf ((eval (sym 'data obj)) idx) value))



(define (change-dbx obj name idx value)

    (setf ((eval (sym name obj)) idx) value))



(Mydb "data" (sequence 11 20))



(define (change-dby obj idx value)

    (setf ((obj "data") idx) value))



(define (change-dbz obj name idx value)

    (setf ((obj (string name)) idx) value))

But these are not the disadvantage, sorry.



By the way,


QuoteUsing catch and throw



As an alternative to catch, the throw-error function can be used to catch errors caused by faulty code or user-initiated exceptions.

throw-error is error-event ?

Lutz

#1
The error with $reader-event occurs only when the macro.lsp module is loaded and a macro is defined. Meanwhile, while investigating, I have taken the macro module offline to repair the event-handler function.



But the last line in the documentation was wrong too, here is the correct version, now also updated in the manual:


;; pass data by context reference

(set 'Mydb:data (sequence 1 100000))

(define (change-db obj idx value)
    (setf (obj:data idx) value))

(change-db Mydb 1234 "abcdefg")

(nth 1234 Mydb:data)   → "abcdefg"   ;<=== corrected
; or
(Mydb:data 1234)   → "abcdefg"

johu

#2
Thank you, Lutz.

I see that the error is due to macro:rewrite.



I succeeded by the following code:


(when macro
  (setq tmp '())
  (swap macro:macro-list tmp))

(define (change-db obj idx value)
    (setf (obj:data idx) value))

(if tmp (swap macro:macro-list tmp))

Lutz

#3
It turns out, that the error was in the symbol compare routines inside newLISP. The error is fixed now in the downloads/development/inprogress/newlisp-10.2.15.tgz code, and the change-db code will work even when macros with macro.lsp are defined.

johu

#4
Thank you for the advice and the rapid work, Lutz.



I updated the Japanese translation of current version manual into rev 16.



http://cid-23a9a25e1aec3626.office.live.com/self.aspx/.Public/newlisp%5E_manual-10208.zip">//http://cid-23a9a25e1aec3626.office.live.com/self.aspx/.Public/newlisp%5E_manual-10208.zip



By the way, in the last example



(nth 123 Mydb:data)   → "abcdefg"

(nth 1234 Mydb:data)   → "abcdefg"



And,



Functional-object oriented programming (FOOP) is based on the following four principles:

Functional-object oriented programming (FOOP) is based on the following five principles:



A target object inside a class-method function is accessed vi the self function.

A target object inside a class-method function is accessed via the self function.



Maybe.