[Prev][Next][Index][Thread]

Closures



I'm new to Dylan and functional programming.  I've been reading
the DRM and "Dylan Programming".

The closures example in the DRM confuses me:

define method make-score (points :: <number>)
   method (increase :: <number>)
     points := points + increase;
   end method;
end method make-score;
define constant score-david = make-score(100)
define constant score-diane = make-score(400)
score-david(0)
fi 100
score-david(10)
fi 110
score-david(10)
fi 120
score-diane(10)
fi 410
score-david(0)
fi 120

I can follow creating new methods with different initial values
of points.  But the methods seem to also have associated
with them a persistent points slot, kind of like a
C local static variable who's value keeps getting
updated each time the method is called.

Somehow this seems to violate Dylan's separation of
objects and functions.  The closure created function
seems to have associated with it an object with slot
points.  What am I missing?

-- Doug




Follow-Ups: