Hello all,
In the standard library, there is a common convention to have two versions
of a lot of functions. The first version takes a reference to some error
state and if an error occurs in the function, it sets the state and
returns. The second version does not have this error state argument and
just halts when an error is occurred. In pseudocode:
proc foo (..., ref err) {
// do some stuff
if (an error occurred) {
err = SOMETHING_BAD_HAPPENED;
return 0;
}
// etc
}
proc foo (...) {
var err = new errorthing;
var ret = foo(..., err);
if err.occurred then
halt(err);
return ret;
}
It seems like the language could benefit from a special compile-time
constant called "undefined" say. Any variable can take this special value,
and you can test (at compile time) if a variable has this value. You can't
do anything else with an undefined value. Then the above would become:
proc foo (..., ref err = undefined) {
// do some stuff
if (an error occurred) {
if err == undefined then halt(.....);
else { err = .....; return 0; }
}
// etc ...
}
This provides an alternative (compile-time) way of providing optional
arguments to functions, default values being the original (run-time) way
--- in a sense it is analogous to the special value "nil" for classes. It
should be more widely useful than the motivating example above.
I suppose for completeness that types could also take this value.
Aside, with this mechanism you could simplify error propagation even
further with a function "propagate(err, ...)" which either calls halt() if
err is undefined or else sets err. Then the example becomes:
proc foo(.., ref err = undefined) {
// do some stuff
if (an error occurred) {
propagate(err, "uh oh");
return 0;
}
// etc
}
Chris
------------------------------------------------------------------------------
Sponsored by Intel(R) XDK
Develop, test and display web and hybrid apps with a single code base.
Download it for free now!
http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk
_______________________________________________
Chapel-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-developers