lua-users home
lua-l archive

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



Tastes differ.

For local variables, because assert needs to be there N times, a constraint is given just once and applies throughout that variable's lifespan (checked at every assignment).

I am a heavy user of assert, or ASSUME as I like to alias it. That's not going anywhere, as if I were in position to change the language we love, to begin with...



Rici Lake kirjoitti 23.10.2006 kello 21.33:


On 23-Oct-06, at 1:11 PM, Asko Kauppi wrote:

The issue is, in larger programs its really benefitial to have the _option_ of using type constraints on your variables, values, and return values. Doing them integrated with the syntax simply hides them more from the eye than doing 'assert' everywhere.

But why hide them? assert is a perfectly good way of making it clear what the function expects:

function extract(str, i, j)
  assert(isString(str))
  assert(isNumber(i) and i >  0 and i <= #str)
  assert(isNumber(j) and j >= i and j <= #str)

  local outside = str:sub(1, i) .. str:sub(j+1)
  local inside = str:sub(i, j)

  assert(#outside + #inside == #str)
  return inside, outside
end