anything like C++ references?
Donn Cave
donn at u.washington.edu
Wed Jul 16 18:45:04 EDT 2003
In article <8sdbhvk5b5ia2onmbca4ajkaidpu3ht9ed at 4ax.com>,
Stephen Horne <intentionally at blank.co.uk> wrote:
...
> I was told today that both Perl and ML have something equivalent to
> pointers. I don't know either language, though. Given the current
> audience, mentioning Perl may be a mistake - but we could look up the
> rationale for including them in ML.
In Objective CAML (if that's the ML they meant, or if this is general
to other ML implementations, I don't know), there is a "ref" type
that is essentially a mutable record with one member.
Variables, if I may use the term loosely, are about the same as in
Haskell - symbols, really, that are equated with some value and
thereafter stand for that value. The value's internal state can
change, if it supports that, but it can't be replaced with a different
value. The only thing that ref adds to this is a notational convenience.
You can say
let a = ref 0 in begin
...
a := !a + 1;
...
end
instead of the semantically equivalent
type 'a myref = {mutable val: 'a}
let a = {val = 0} in begin
...
a.val <- a.val + 1;
...
end
Actually it's rather hard to see a great benefit there. But at
any rate, this feature isn't fully equivalent to a hypothetical
pointer in Python - it doesn't point to a location, it is just
a mutable container.
Donn Cave, donn at u.washington.edu
More information about the Python-list
mailing list