• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

'get out' a variable from a sub, keeping it 'local'

  • Thread starter Thread starter x11start
  • Start date Start date

x11start

Guest
in recent times I have been trying to transform my lisp into a sort of "mosaic", in which the various colored tiles are represented by as many independent "modules" that perform a specific function.
this allows me to have a better management of the variables, as each module needs "internal" variables (and therefore 'local': they return to be nil when they exit from the 'module'); of variables in "input": i.e. the variables passed by the main sub... and the variables out.
The latter represent a small problem: they must be 'globals', so that they can use them once they are out of the sub.
I immediately thought I'd let her precede the '&' symbol to recognize them immediately; by virtue of the fact that autolisp is very 'tolerant' with names and does not require that they have specific characters. This solution is not the optimal solution, as there are still variables defined in one place, which then must be placed nil in another!
analyzing more calmly the lisp of the 'guru' of this language.... I found out (!) that if in my 'module' (the sub or the 'text of the mosaic', which I will say), I write a variable. . simple, alone, without any command; this can be "high" from the outside, then continuing to stay nil once out of the sub!

"big! you discovered hot water!" clap clap clap (ironically applause!). .

It doesn't matter even if I've been doing programs in lisp for years, to this (and how useful it was!) I never thought about it, so I want to share it with you!

I'll explain better.


(defun x11:gstring (bit msg def / txt)
(setq txt (getstring bit msg)); input string
(if (= txt) (setq txt def) ; if txt=nil => txt = default)

; output variable:


)

speculating to save this code in a file called gstring.lsp, then upload it with (load "gstring") and launch it by giving it some parameters incoming:

(x11:gstring bit msg def)

in which : bit = t (accept space) - bit = nil does not accept it (it already takes it as sending)
msg = message string for question:
typically starts with "\n to go to boss and term. with default <si> "
es. "\nindicates the city <roma> " space after > to leave question mark.

def = default value (if bit = nil): value that substit to the obtained by sending

e.g.
(x11:gstring nil "\nindicates the city <roma> "Roman"

However in the sub x11:gstring, there are no 'global' variables: the txt variable, which represents the result to be released, is made 'local' by writing the name after the '/' symbol.
but there is a particular: I put the variable txt, 'nude and raw' under the comment 'variable out:'.
This allows me to have it when I get out of the sub!

if in fact I call my sub with:

(setq name (x11:gstring nil "\nindicates the city <roma> "Roman"

I can do so that now the name variable has the value written by the sub (e.g. "roma"), while the txt variable continues to apply nil, as it assumed the value only within x11:gstring.

obviously the 'output' variable can be 1 only... but in lisp there are lists!

I hope this can be useful to someone.</roma></roma></roma></si>
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top