Determine the condition of a function call by another function

Determine the condition of a function call by another function

Browning_Zed
Advocate Advocate
553 Views
5 Replies
Message 1 of 6

Determine the condition of a function call by another function

Browning_Zed
Advocate
Advocate

Greetings to all,
I have the following question. Is there some method for determining the condition of calling one function from another function? I'll explain what I mean.
Let's say there are some three functions: (fA) (fB) (fC).
Now the question. Is it possible inside the function (fA) to set a condition like:
(defun fA nil
    (if <function (fA) will be called from function (fB) then>
        <execute expression 1>
        (if <function (fA) will be called from function (fC) then>
            <execute expression 2>
        )
    )
)

0 Likes
Accepted solutions (1)
554 Views
5 Replies
Replies (5)
Message 2 of 6

CodeDing
Advisor
Advisor
Accepted solution

@Browning_Zed ,

 

There is no "reaction" style process in autolisp. So what you are asking directly is not possible.. what you should be doing instead is executing your statements inside your "fB" and "fC" functions..

 

To Explain... Instead of:

(defun fA nil
    (if <function (fA) will be called from function (fB) then>
        <execute expression 1>
        (if <function (fA) will be called from function (fC) then>
            <execute expression 2>
        )
    )
)

 

You should be doing:

(defun fB nil
.....
<execute expression 1> ...OR...
(call function (f?) which executes expression 1)
)
(defun fC nil
.....
<execute expression 2> ...OR...
(call function (f?) which executes expression 2)
)

 

Best,

~DD

Message 3 of 6

martti.halminen
Collaborator
Collaborator

More generally, in any programming language, functions knowing anything about the internals or behaviour of other functions is a bad idea, as that would require that any changes are updated at both places.

 

In Lisp especially so: Lisp is a dynamically linking language, so the definition of any function may change even at runtime.

 

As far as possible, a function should only know whatever it gets as its parameters, and only be visible to the outside via whatever it returns.

- obviously not always possible, there are global variables and globally maintained data structures, and side effects like drawing something on the screen are occasionally desirable.

 

In Visual Lisp there is a way of noticing some actions at runtime. See the documentation about reactors, especially VLR-LISP-REACTOR.

- though if you can work without these, life will be much simpler.

 

 

0 Likes
Message 4 of 6

1LandSurveyor
Enthusiast
Enthusiast

Why would you simply not add a mode input variable in your function A that would specify the expression to use?

 

(defun fA ( mode / )
    (cond ((= mode 0) <execute expression 1> )
          ((= mode 1) <execute expression 2> )
           ; ...
          ((= mode X) <execute expression Y> )
    )
)

 

in your other command, you would simply launch the functionA specifying the desired mode using (fA 0)  or (fA 1) and so on...

Message 5 of 6

martti.halminen
Collaborator
Collaborator

I'd prefer a style like this:

 

(defun fA ( mode / )
    (cond ((eq mode 'initializing) <execute expression 1> )
          ((eq mode 'drawing) <execute expression 2> )
           ; ...
          ((eq mode 'finishing) <execute expression Y> )))

 

Saves looking up in some table what is the meaning of mode 8 in this context by using human-readable mode names.

- may take a few milliseconds more compile time, but runtime speed is the same as when using numbers.

Message 6 of 6

1LandSurveyor
Enthusiast
Enthusiast
I totally agree. If logical names can be used, it’s the way to go.
0 Likes