Re: Making a function "forget" a variable



Barry Margolin <barmar@xxxxxxxxxxxx> writes:

In article <7cd4ppvf4t.fsf@xxxxxxxxxxxxxxxxxxxxxxx>,
pjb@xxxxxxxxxxxxxxxxx (Pascal J. Bourguignon) wrote:

Barry Margolin <barmar@xxxxxxxxxxxx> writes:

In article <ymive3ie04y.fsf@xxxxxxxxxxxxxxxx>,
tar@xxxxxxxxxxxxx (Thomas A. Russ) wrote:

Spiros Bousbouras <spibou@xxxxxxxxx> writes:

It's a perfectly serious question. As for real example
I don't have one ; it was a theoretical question. In
practice I would only be slightly worried of inadvertENtly
using a variable where it shouldn't occur. Nevertheless
having a way to make each function know only about the
variables it's supposed to be using and nothing more
satisfies me from an aesthetical point of view, adds a
little bit of safety, and leads to self-documenting code.

Well, the simplest way to do this would be to actually pass those
variables in as function arguments. That's the traditional way to make
sure that functions don't use arguments they aren't supposed to.

Doesn't work if the function needs to update the variable.

Really?

While your technique is certainly cute, I hardly think it's what he
meant by "pass those variables in as function arguments".

That's exactly what's done in C. What would you call that if it was
written in C?

(defmacro & (var)
(let ((m (gensym)) (v (gensym)))
`(lambda (,m &optional ,v)
(ecase ,m ((get) ,var) ((set) (setf ,var ,v))))))
(defun deref (locative) (funcall locative 'get )) ; sorry,
I swaped the first
(defun (setf deref) (value locative) (funcall locative 'set value)) ; two
argument in my other post.

(defun f1 (*v1)
(incf (deref *v1)))

(defun f2 (*v2)
(decf (deref *v2)))

(let ((v1 10) (v2 20))
(f1 (& v1))
(f2 (& v2))
(list v1 v2))


void f1(int *v1){
--(*v1);
}

void f2(int *v2){
++(*v2);
}


list_t* example(void){
int v1=10;
int v2=20;
f1(&v1);
f2(&v2);
return(list(v1,v2));
}

--
__Pascal Bourguignon__ http://www.informatimago.com/
Wanna go outside.
Oh, no! Help! I got outside!
Let me back inside!
.



Relevant Pages

  • Re: C Programming
    ... > reason for why it matters. ... There are books around that carefully do not teach bad practice, ... this book doesn't use void main. ... is int mainor int main. ...
    (Fedora)
  • Help in Java swings(internal Frame)
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)
  • [PATCH] get rid if __cpuinit and __cpuexit
    ... unsigned long action, void *hcpu) ... unsigned int cpu = hcpu; ... -static int __cpuinit ... __cpu_up(unsigned int cpu) ...
    (Linux-Kernel)
  • [PATCH,RFC 2.6.14 09/15] KGDB: SuperH-specific changes
    ... This adds basic support for KGDB on SuperH as well as adding some architecture ... -static int kgdb_uart_getchar ... -static void kgdb_uart_putchar ... * The command-line option can include a serial port specification ...
    (Linux-Kernel)
  • problem in java swings
    ... public int getSize() ... public void valueChanged{ ... private JScrollPane scrollPane1; ... public class PeakContainer extends JInternalFrame ...
    (comp.lang.java.programmer)