lua-users home
lua-l archive

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


> > Markus Huber <pulse@evolution.org>

> > string.byte('')  --> bad argument #2 to `byte' (out of range)
> > I don't feel that an empty string is an bad argument. I think to
> > return nil in this case is more usefull because: ...

> Tomas <tomas@rpa.vrac.puc-rio.br>

> It isn't. The bad argument is nil where you should use a number to
> indicate the position at the string.  See the manual:

> string.byte (s [, i])

I think you know what [] means? Optional. There is no argument #2 so it
can't be bad! In fakt argument #1 is bad in this case because its an
empty string. But please back to the question: What is more practically?

print(string.byte(''))  --> nil   ?
print(string.byte(''))  --> error ?

Same with filled string but "out of range" position:

print(string.byte('Hello',10))  --> nil   ?
print(string.byte('Hello',10))  --> error ?

I think error is useless here and not conform with the standard
behaviour e.g. print(string.sub('Hello',10,20)) returns an empty string
and not an stupid "out of range" error. But nil is perfect to vaidate
the existence and if so the ASCII-Code in one simple if ... end
construct.

I have patched it for me now.

  local Function = string.byte

   function string.byte(String,Position)

      Position=Position or 1

      if math.abs(Position)>string.len(String) then
         return nil
      else
         return Function(String,Position)
      end

   end

It's an error or not? Its often an challenging question and a free
decision of the programmer/designer. I feel nil return should be the
standard behaviour. Errors is a given number instead of a string and
such things. Also I feel its very important for Lua to design
things practically and without knick-knack. 


--
Markus