3 ms·
Well... using the example from the linked article, it could be made equivalent to your Objective-C snippet, provided the optional passed error-object/struct isn
by cnvogel 12y ago
Well... using the example from the linked article, it could be made equivalent to your Objective-C snippet, provided the optional passed error-object/struct isn't updated only on errors (as the article suggests), but also can be used to find out if an error happened in the first place.
With a suitable modification, the mentioned example would have to be changed from...
libfoo_widget_container_t container = NULL;
libfoo_error_details_t error = NULL;
if (libfoo_create_widgets(12, &container, &error) != libfoo_success) {
printf("Error creating widgets: %s\n", libfoo_error_details_c_str(error));
libfoo_error_details_free(error);
abort(); // goodbye, cruel world!
}
...to...
libfoo_widget_container_t container = NULL;
libfoo_error_details_t error = NULL;
libfoo_create_widgets(12, &container, &error);
if (LIBFOO_IS_BAD(error)) {
printf("Error creating widgets: %s\n", libfoo_error_details_c_str(error));
libfoo_error_details_free(error);
abort(); // goodbye, cruel world!
}
where LIBFOO_IS_BAD() would be a macro or inline function inspecting the error object/struct, and might be something like
#define LIBFOO_IS_BAD(error) ((error) && (error)->errno != 0)
Cutting out the cruft, it's basically what you had suggested to pre pretty Objective-C style ;-)
/* your example */
NSError* error = nil;
[object doSomething: foo error: &error];
if(error){
}
/* article */
libfoo_error_details_t error = NULL;
libfoo_create_widgets(12, &container, &error);
if (LIBFOO_IS_BAD(error)) {
}
EDIT: The linked article already mentions that in the example, error would only be updated if the function returned something unlinke libfoo_success, so one could directly convert to your favourite style keeping these semantics!