lua-users home
lua-l archive

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


I don't know if this would be regarded as a "good practice" but  you
could just use metatables to create a __call method for strings and
make it behave like string.byte does. Therefore you can:

local mah_string = "hello"
print( mah_string(1) )

I would provide an example but a quick search in the manual (I'm quite
new to lua...) has led me to this:

"You can replace the metatable of tables through the setmetatable
function. You cannot change the metatable of other types from Lua
(except by using the debug library); you must use the C API for that."

And I suck at using the C API so I'll just leave the idea here. Please
correct me if I'm wrong and  it's not possible to use metatables for
that purpose :)

2010/12/19 Dirk Laurie <dpl@sun.ac.za>:
> On Sun, Dec 19, 2010 at 07:10:00PM +0200, David Kastrup wrote:
>> Dirk Laurie <dpl@sun.ac.za> writes:
>>
>> >>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?  If not (yet)
>> > in the Lua language, then at least in the terminology used by
>> > the author of the reference manual?
>>
>> That kind of logic leads to Cobol.
>
> The logic is as follows:
>
> 1. There are many situations in which a concise notation for
>    the i-th character of a string s is useful, including the
>    description of what string.byte does.
> 2. The notation s[i] for that purpose is so universally
>    understood that it is used without further ado in the
>    Lua Reference Manual.
> 3. It would therefore not be an obscure notation in the Lua
>    language itself.
>
> I find the reference to Cobol totally incomprehensible.
> Cobol is a language that over-emphasizes the use of English
> words rather than notation based on symbols, thus:
>
> ADD SALARY_INCREMENT TO CURRENT_SALARY GIVING NEW_SALARY
>
> It is Cobolic to write "string.sub(i,i)" rather than "s[i]".
>
> Dirk
>
>
>



-- 
http://www.twitter.com/lfzawacki
http://www.linesocode.wordpress.com
http://www.umblag.wordpress.com