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

Closures



I'm new to Dylan and many of the concepts.  In the
Dylan Reference Manual is an example that I can't
follow.  It seems to me that there are errors in
the text.  Am I missing something?

> The following example defines a function that returns score-card methods.
> The method that is returned is closed over the score parameter. Each time
> this method is called, it updates the score parameter and returns its new value.
> 
> 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
> 
> Each invocation of make-score creates a new binding for score, so each 
> closure returned by make-score refers to a different binding. In this way, 
> assignments to the variable made by one closure do not affect the value of 
> the variable visible to other closures.

I don't see a "score parameter" anywhere.  Does it mean "points"?

Could someone give a blow by blow of how the above works?

-- Doug




Follow-Ups: