[Nickle] Funny situation
Keith Packard
keithp at keithp.com
Sat Jun 28 12:28:59 PDT 2003
Around 0 o'clock on Jun 28, Bart Massey wrote:
> What am I doing wrong here? Or is this a Nickle bug?
Recall that reference types are just syntactic sugar for pointer types:
...
public typedef &elem list;
...
public list empty = &nilcell;
...
list l = empty;
Using the reference rules, we rewrite this as:
public typedef *elem list;
public list empty = &nilcell;
list l = *empty;
As you can see, there is now a type mismatch -- reference types make
naming the referenced value easy and naming the referring value hard. You
can "fix" this by using & here:
list l = ∅
That's because initializers for reference variables are a bit mystic --
they compute the referring value rather than the referenced value.
To make this datatype work like you expect, you can either use pointers or
you can move the union:
namespace List {
public typedef list;
typedef elem;
public typedef union {
&elem cell;
void nil;
} list;
typedef struct {
poly first;
list rest;
} elem;
public list cons(poly v, list l) {
return (list) { cell = reference ((elem) { first = v, rest = l })};
}
public poly car(list l) {
return l.cell.first;
}
public list cdr(list l) {
return l.cell.rest;
}
public bool null(list l) {
union switch(l) {
case cell c:
return false;
case nil:
return true;
}
}
public list empty = { nil = <> };
}
-keith
More information about the Nickle
mailing list