Throwing specific Error Obejct for asserts
monarch_dodra
monarchdodra at gmail.com
Thu Jan 10 06:23:55 PST 2013
Now that we have version assert, we can customize the assert flow
to throw a more specific error:
Before:
//----
auto opSlice(size_t i, size_t j)
{
assert(i <= j);
}
//----
After:
//----
auto opSlice(size_t i, size_t j)
{
version(assert)
if (i > j)
throw new RangeError();
}
//----
Now, instead of having a generic AssertError (with or without a
message), we have a statically typed RangeError. Yay!
I have 2 problems with this though:
1) It is verbose as shit.
2) Using a if shifts the logic from checking the valid condition,
to checking if things are *in*valid condition. eg: (i <= j) vs (i
> j). If find this change of flow VERY disruptive. And error
prone if migrating a condition that contains "&&" or "||".
So question: Why don't we have, just like for enforce, the
possibility of simply writing:
//----
assert(i <= j, new RangeError());
//----
Seems like a win-win to me...
...Or would this be disruptive with things like "onAssertError"
or "setAssertHandler" ... ?
More information about the Digitalmars-d
mailing list