Go Programming talk [OT]

Adam Ruppe destructionator at gmail.com
Mon Jun 7 08:30:42 PDT 2010


On 6/7/10, Leandro Lucarella <llucax at gmail.com> wrote:
> Yes, they are not implemented exactly the same, but the concept is very
> similar. And I agree that scope is really a life saver, it makes life
> much easier and code much more readable.

There is one important difference though: Go doesn't seem to have
scope(failure) vs scope(success). I guess it doesn't have exceptions,
so it is moot, but it looks to me like suckage.

Take some recent code I wrote:

void bid(MySql db, Money amount) {
   db.query("START TRANSACTION");
   scope(success) db.query("COMMIT");
   scope(failure) db.query("ROLLBACK");

   // moderate complex logic of verifying and storing the bid, written
as a simple linear block of code, with the faith that the scope guard
and exceptions keep everything sane
}

Just beautiful, that scales in complexity and leaves no error
unhandled. Looks like in Go, you'd be stuck mucking up the main logic
with return value checks, then use a goto fail; like pattern, which is
bah. It works reasonably well, but leaves potential for gaps in the
checking, uglies up the work code, and might have side effects on
variables. (The Go spec says goto isn't allowed to skip a variable
declaration... so when I do:

auto result = db.query();
if(result.failed) goto error; // refuses to compile thanks to the next line!
auto otherResult = db.query();

    error:

Ew, gross.).

That sucks hard. I prefer it to finally{} though, since finally
doesn't scale as well in code complexity (it'd do fine in this case,
but not if there were nested transactions), but both suck compared to
the scalable, beautiful, and *correct* elegance of D's scope guards.

That said, of course, Go's defer /is/ better than nothing, and it does
have goto, so it is a step up from C. But it is leagues behind D.


More information about the Digitalmars-d mailing list