lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tuesday, September 23, 2014 01:09:36 PM Daurnimator wrote:
> This seems to be asking for some form of selective 'catch'.
> (as in, try{} catch(Exceptions.NOMEM) {}

Not sure I like the attempt to sneak static typing into a dynamic language. 

I tried thinking of some way to reduce the boilerplate needed to handle erros 
but what I thought of basically boiled down to

   local this,errmsg = dosomething()
   if not this then
     -- handle error with errmsg
   end

Anything simpler than that would end up looking like BASIC's "on error goto" 
statement and that way madness lies.

You could make make the error handler a closure, like:

    function assert(onerror, ...)
      if not ... then onerror() else return ... end
    end

But now you're writing the error handler before the values it tests, which 
seems backward to me. And you lose the ability to use goto or return from the 
function. Perhaps if assert were a primitive you could move the function block 
to the end as:

    assert local this,errmsg = dosomething() do
      -- handle error
    end

Yet that is not significantly different than the simple if-then written above, 
but with the added confusion of someone typing '=' when they meant '=='. And 
if you insist that the assert condition be an expression, then it's exactly 
the same as an 'if'.

That also looks like Ruby, but can you imagine the confusion if a 'goto' 
wouldn't work because the block has magically been turned into a function? 
Maybe that's what we really need, the ability to jump to labels in an outer 
function.

-- 
tom <telliamed@whoopdedo.org>