[Nickle]Nickle open() returns 0 on error

Bart Massey nickle@nickle.org
Sun, 14 Jul 2002 03:47:23 -0700


------- =_aaaaaaaaaa0
Content-Type: text/plain; charset="us-ascii"
Content-ID: <23548.1026643612.1@bart.cs.pdx.edu>

The Nickle open() function currently returns integer 0 on
error.  This is obviously borken, as e.g.
  file f = open("/notthere", "r");
will throw a type exception during the assignment instead of
doing something sensible.

Alternatives I see offhand are to (1) make open() itself
throw an exception, or to (2) make open return a structured
type of either fid(file) or badfile.  It seems clear to me
that (1) is the right answer here.

Assuming that I reimplement open() to do (1), is the
attached code roughly how I would use twixt with try to get
using this to work smoothly?  (As if I shouldn't know. :-)


BTW, in the dept. of bad ideas

  exception x();
  try {raise x();} catch y() { printf("darn\n"); };

actually doesn't do anything: no unhandled exception x(), no
"darn", nothing.  IIRC this had something to do with y()
being treated as a function definition, but in any case,
I think things are either buggy or mis-speced here.  The
behavior I think I want:
  + catch must be supplied with an in-scope exception name
    of the correct arity and compatible arg-types.
I think we agreed there was no wildcard catch().  The
real problem here, of course, is if you slightly mis-spell
the exception name.


It's late.  I'm sleepy.  Goodnight.

	Bart


------- =_aaaaaaaaaa0
Content-Type: text/plain; charset="us-ascii"
Content-ID: <23548.1026643612.2@bart.cs.pdx.edu>

#!/usr/bin/env nickle

string name = "/notthere";

import File;

exception io_exception(string msg);

file myopen(string fn, string m) {
  printf("in myopen\n");
  poly result = open(fn, m);
  if (result == 0) {
    printf("oops\n");
    raise io_exception("could not open/create file");
  }
  return result;
}

printf("starting\n");

try twixt(file f = myopen(name, "r"); close(f)) {
  printf("body\n");
  string s;
  fscanf(f, "%s", &s);
  printf("got %s\n", s);
} catch io_exception(string msg) {
  fprintf(stderr, "%s: %s\n", name, msg);
  exit(1);
};

------- =_aaaaaaaaaa0--