lua-users home
lua-l archive

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


On Sun, Dec 19, 2010 at 12:03 PM, Dirk Laurie <dpl@sun.ac.za> wrote:
> >From the Reference Manual, description of the function string.byte:
>
> [[
>    Returns the internal numerical codes of the characters
>    s[i], s[i+1], ···, s[j]
> ]]
>
> What better proof do we need that s[i] means the character
> corresponding to the i-th byte of the string s?

A subtle point here:  This snippet from the manual is talking about
the *character* at s[1], and Lua doesn't have a character type.  So
for Lua to support this, it would either need to add a character type,
which won't happen, or it has to pick a surrogate for this type.  The
only sensible surrogates are one-byte strings, and integers.

I think using integers here is a non-starter because of
number-to-string coercion.  The following is just way too weird:
  s = 'hi'; return s[1] .. s[2]  -- would return '104105'

Using single-byte strings is a cheat.  There's something awfully
impure about pretending that a string is a read-only mapping from
integers to strings.  This can be a convenient fiction, and other
languages do it with little trouble, but it's still dirty.  (Why
should s[1] == s[1][1][1][1]?)

Lua is a pretty tidy language.  Since there's no good choice for what
s[i] should return, and since it's trivial to extend Lua to have
either of the above behaviors if you want it, the lack of string
indexing in Lua seems entirely natural and unsurprising (at least to
me).

Greg F