[Nickle] Case labels

bart at po8.org bart at po8.org
Thu Sep 16 01:39:18 PDT 2004


I started thinking about the situation with union types and
case labels and subtyping after Keith and I had a
conversation earlier this evening.  I came up with this
unfortunate case.

  $ nickle
  > typedef enum {blue} bt;
  > typedef enum {blue, green} bgt;
  >
  > void foo(bt v) { union switch (v) { case blue: printf("ok\n"); } }
  > bt p;
  > p.blue = <>;
  > foo(p)
  ok
  > bgt p;
  > p.blue = <>;
  > foo(p)
  ok
  > p.green = <>;
  > foo(p)
  ok
  > p
  green = <>

Note the result of the final invocation of foo().  The type
semantics of Nickle as I understand them suggest that this
should be statically OK, but dynamically should fail to
print anything.  Continuing:

  > void bar(bgt v) { union switch (v) { case blue: printf("blue\n"); case green: printf("green\n");} }
  > bar(p)
  green
  > bt p;
  > p.blue = <>;
  > bar (p)
  Unhandled exception invalid_argument ("Incompatible argument", 0, blue = <>)
  <stdin>:18:     bar (p);

The static typechecker accepts this.  Should it execute or
throw an exception?  Quite confusing.

	Bart



More information about the Nickle mailing list