Variables and closures (Was: Objects and Relations)
Date: 9 Feb 2007 10:42:30 -0800
Message-ID: <1171046550.699031.140890_at_s48g2000cws.googlegroups.com>
On Feb 8, 11:05 pm, "David BL" <davi..._at_iinet.net.au> wrote:
>
> In common usage 'variable' is only used with reference to the
> identifiers in the source that at run time are associated with an
> instance of some type (whether a global variable, frame variable or
> member variable).
That's certainly the common usage in the C++ and Java worlds, which is perhaps unsurprising since they have specific terminology for their first-class variable construct ("object") and there is no relationship in the language semantics between their first class variables and their second class variables (which they call "variables.")
This article descibes the notion of a "first class" construct
as the term is used in programming language theory:
http://en.wikipedia.org/wiki/First-class_object
(The word "object" in the title above is the generic
sense of the word, not the OOPL sense.)
In SML, the notion of a name and the notion of a
variable are completely decoupled. Names
are always bound to constants, however that
constant might be the identity of a variable.
Relational language discussions generally omit the
notion of first class variables, because they aren't
a great fit with relational semantics. For that reason
I've mostly tried to avoid the notion of first class
variables (as well as objects) in my own designs,
however I've found it impossible to fully discard the
notion of first class variables, because they crop up
almost inevitably once we put together two ideas
each of which are "must-haves" in my humble opinion:
lexical scope and first-class functions.
Consider:
function make_sequence(initial_value:int):int {
var x:int = initial_value;
The nested function sequencer() closes over the variable
x declared in the outher scope, a natural consequence of
lexical scope. The function make_sequence returns a
sequencer which is bound to a specific instance of x.
Once we put these language features together, we are
faced with a nasty choice: we have to either put some
function sequencer():int {
return x++;
}
return sequencer;
}
Marshall Received on Fri Feb 09 2007 - 19:42:30 CET