Persistent structures inside function definitions

Freeform discussion about anything related to modding Transcendence.
Post Reply
giantcabbage
Militia Lieutenant
Militia Lieutenant
Posts: 107
Joined: Thu Apr 07, 2011 9:05 pm

Structures defined in a lambda block behave as static local variables - i.e. the same structure reference is used every time the function is called. Is this the intended behaviour? This feature could be useful, but seems surprising as lists do not do this

Code: Select all

(setq struct (lambda (key value)
    (block (theStruct)
        ; theStruct is always Nil at this point
        (setq theStruct {})
        ; theStruct is the empty struct only on the first call
        (setItem theStruct key value)
        theStruct
    )
))

; First call to function behaves as expected:
(struct "test" 4) -> {test:4}

; Second call returns same structure reference:
(struct "same" 5) -> {test:4 same:5}
note: I can get the results I expected by using (setq theStruct '{})
george moromisato
Developer
Developer
Posts: 2998
Joined: Thu Jul 24, 2003 9:53 pm
Contact:

Definitely not what I would have expected either.

I'll take a look.
george moromisato
Developer
Developer
Posts: 2998
Joined: Thu Jul 24, 2003 9:53 pm
Contact:

I've fixed it in 1.08. For now, quoting a literal structure is a good work around:

Code: Select all

(setq theStruct '{})
Post Reply