[Nickle] Reference semantics are confusing in Nickle
Keith Packard
keithp at keithp.com
Sun Nov 28 14:09:06 PST 2004
Around 15 o'clock on Nov 27, "James LaMar" wrote:
> > &int i = reference(5)
> &5
You can also just use
> &int i = &5;
The box operator '&' works fine on rvalues.
> -> j[5] = reference (5);
> <stdin>:4: Incompatible types, left 'int', right '&poly', for =
> operation
>
> Why is this ill-typed?
Remember that references are just syntactic sugar for pointers. In this
case, j[5] doesn't refer to the reference at j[5], but rather the value
pointed at by that reference. Think of any reference expression as the
equivalent pointer expression with a '*' prepended, and any reference
expression with '&' as the bare pointer expression.
int i;
int j;
&int r = &i;
*int p = &i;
r = 5 <=> *p = 5
&r = &j; <=> p = &j;
So, to store into the array of references, you do:
&j[5] = &3;
(I note here the pleasing symmetry of the box operator...)
> > j[5] = 5
> Unhandled exception uninitialized_value ("uninitialized hash element")
> <stdin>:5: j[5] = 5;
>
> Why doesn't this work?
Same reason; j[5] dereferences the value at j[5], which hasn't been
initialized yet.
> &j[5] = &0
> j[5] = 5
works just fine.
The examples with arrays appear identical to the above discussion with
hashes.
-keith
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
Url : /pipermail/nickle/attachments/20041128/c1607857/attachment.pgp
More information about the Nickle
mailing list