lua-users home
lua-l archive

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


On Mon, Dec 13, 2010 at 1:20 PM, Petite Abeille <petite_abeille@mac.com> wrote:
>
> On Dec 13, 2010, at 10:06 PM, Javier Guerra Giraldez wrote:
>
>> (PS: to answer PA, that would break current  (''):find() usage )
>
> Yes, yes... I know it's dumb... but since when are we in the business of preventing people from shooting themselves in the foot?!? :P
>
> getmetatable( '' )[ '__call' ] = function( self, a, b ) return self:sub( a, b ) end
> getmetatable( '' )[ '__index' ] = function( self, key ) if type( key ) == 'number' then return string.char( string.byte( self, key ) ) end return string[ key ] end
>
> print( #aString, aString[ 1 ] )
> print( #aString, aString( 1, 2 ) )
> print( #aString, aString:upper() )
>
>> 3     a
>> 3     ab
>> 3     ABC
>
>

If you're going to use __call, why not drop __index altogether? I
don't really like having two different syntaxes for what's arguably
the same operation.

~Jonathan