[Nickle] Reference semantics are confusing in Nickle
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)
You can also just use
> &int i = &5;
The box operator '&' works fine on rvalues.
> -> j = reference (5);
> <stdin>:4: Incompatible types, left 'int', right '&poly', for =
> Why is this ill-typed?
Remember that references are just syntactic sugar for pointers. In this
case, j doesn't refer to the reference at j, 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 r = &i;
*int p = &i;
r = 5 <=> *p = 5
&r = &j; <=> p = &j;
So, to store into the array of references, you do:
&j = &3;
(I note here the pleasing symmetry of the box operator...)
> > j = 5
> Unhandled exception uninitialized_value ("uninitialized hash element")
> <stdin>:5: j = 5;
> Why doesn't this work?
Same reason; j dereferences the value at j, which hasn't been
> &j = &0
> j = 5
works just fine.
The examples with arrays appear identical to the above discussion with
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 228 bytes
Desc: not available
Url : /pipermail/nickle/attachments/20041128/c1607857/attachment.pgp
More information about the Nickle