[Prev][Next][Index][Thread]
Closures
-
To: info-dylan@ai.mit.edu
-
Subject: Closures
-
From: Doug Hockin <dhockin@staffware-spokane.com>
-
Date: Thu, 9 Aug 2001 00:15:01 -0400 (EDT)
-
Organization: Concentric Internet Services
-
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.2) Gecko/20010726 Netscape6/6.1
-
Xref: traf.lcs.mit.edu comp.lang.dylan:13567
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: