why scope(success)?
Daniel Keep
daniel.keep.lists at gmail.com
Wed May 10 23:20:44 PDT 2006
I've been writing an OO binding to the cairo 2D drawing API, and
scope(success) has been a huge help.
Basically, cairo's api functions rarely if ever return an error code.
Instead, they set a flag on the object's pointer itself[1]_. Since I
need to check this flag after every call I make on any particular
object, each wrapper class has a protected "checkStatus" member. Then,
I just need to write this:
void someMember()
{
scope(success) checkStatus();
// ... do cairo stuff ...
}
When the "do cairo stuff" is just a simple function call, I don't really
gain anything. But it comes in handy when I've got to return values
from cairo functions, or when I have to use any kind of complex logic.
Plus, I find reading "scope(success) checkStatus()" to be very clear;
that, and it's immediately obvious just looking at the code if I've
forgotten it (almost every function in the binding follows that pattern
so exceptions to that are easily spotted).
-- Daniel
.. [1]: When I say "object" what I mean is that objects in cairo are
just pointers to undefined structs. So a "cairo_pattern_t*" is an
"object" :P
P.S. Cx looks very cool; must have a poke around that...
Ben Hinkle wrote:
> I hope this doesn't come of as a flame, but I'm wondering if anyone is using
> scope(success) and why. I can't find any reason for it.
>
> Some background: I've slowed my D work to focus on some C experimental
> features I'm calling Cx: http://www.tinycx.org and currently I'm
> implementing the error handling using reserved labels "error:" and
> "finally:". The error label is roughly like scope(failure) and the finally
> label is roughly like scope(exit). There's no try-catch-finally. I don't
> plan on adding anything like scope(success) because I couldn't think of why
> anyone would want to use it. Why not just put the code at the end of the
> scope like normal code-flow? I suppose one could code the entire scope in
> reverse just for kicks:
> void main() {
> scope(success) printf("world\n");
> scope(success) printf("hello ");
> }
>
> -Ben
>
>
--
v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D
a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d
mailing list