lua-users home
lua-l archive

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


It was thus said that the Great Dirk Laurie once stated:
> Look at this:
> 
> > print(pcall(0,1,2))
> 
> false    attempt to call a number value
> 
> I.e. `pcall` can't fail. My program retains control.
> 
> But now this:
> 
> > print(rawget(0,1,2))
> stdin:1: bad argument #1 to 'rawget' (table expected, got number)
> 
> To get something non-failing, I need to wrap the 'rawget' in a test inside
> a test for table-ness.
> 
> Next time this annoys me enough, I'll write a little module in C that
> provides `prawget` and `prawset`, which stand in the same relationship
> to `rawget` and `rawset` as `pcall` stands to `call`.
> 
> Unless someone has already done it?

Um ...

function prawget(tab,idx)
  return pcall(rawget,tab,idx)
end

print(prawget(0,1))
print("done")

  Seems simple enough ... 

  -spc