lpeg question

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

lpeg question

Dibyendu Majumdar
Hi,

I found that Ravi cannot execute lpeg tests. Upon further
investigation, it seems the issue is because Ravi expects '#' operator
to return an integer result, but lpeg defines a metamethod for this
operator that returns a userdata object.

Is this a violation of the contract for # operator, or is my
understanding incorrect about the result of this operator?

Thanks and Regards
Dibyendu

Reply | Threaded
Open this post in threaded view
|

Re: lpeg question

Charles Heywood
To quote the manual:

> A program can modify the behavior of the length operator for any value but strings through the __len metamethod (see §2.4).

This does not require that the value be an integer, but instead "any value".

On Tue, Mar 13, 2018 at 3:22 PM Dibyendu Majumdar <[hidden email]> wrote:
Hi,

I found that Ravi cannot execute lpeg tests. Upon further
investigation, it seems the issue is because Ravi expects '#' operator
to return an integer result, but lpeg defines a metamethod for this
operator that returns a userdata object.

Is this a violation of the contract for # operator, or is my
understanding incorrect about the result of this operator?

Thanks and Regards
Dibyendu

--
--
Ryan | Charles <[hidden email]>
Software Developer / System Administrator
Reply | Threaded
Open this post in threaded view
|

Re: lpeg question

Dibyendu Majumdar
On 13 March 2018 at 20:25, Charles Heywood <[hidden email]> wrote:

> On Tue, Mar 13, 2018 at 3:22 PM Dibyendu Majumdar <[hidden email]>
>> I found that Ravi cannot execute lpeg tests. Upon further
>> investigation, it seems the issue is because Ravi expects '#' operator
>> to return an integer result, but lpeg defines a metamethod for this
>> operator that returns a userdata object.
>>
>> Is this a violation of the contract for # operator, or is my
>> understanding incorrect about the result of this operator?
>>
> To quote the manual:
>
>> A program can modify the behavior of the length operator for any value but
>> strings through the __len metamethod (see §2.4).
>
> This does not require that the value be an integer, but instead "any value".
>

I don't interpret the manual that way. I read that as saying that you
cannot modify the length operator for a string. But __len is defined
as a length operation, is it not?

And above is in section 3.4.7 by the way.

Reply | Threaded
Open this post in threaded view
|

Re: lpeg question

Roberto Ierusalimschy
> I don't interpret the manual that way. I read that as saying that you
> cannot modify the length operator for a string. But __len is defined
> as a length operation, is it not?

I am not sure what you mean by "__len is defined as a length operation".
It is *called* the length operator, but a name does not give its
semantics. __add is also related to the "addition operation", but that
name does not imply that it is an addition.

Note the following:

1) The __eq metamethod, which always returns a boolean, says so:

  "The result of the call is always converted to a boolean."

2) The API for the length operation does not return a number, but a
Lua value. (The same is true for addition, but not for comparisons.)

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: lpeg question

Dirk Laurie-2
In reply to this post by Dibyendu Majumdar
2018-03-13 22:33 GMT+02:00 Dibyendu Majumdar <[hidden email]>:

> I don't interpret the manual that way. I read that as saying that you
> cannot modify the length operator for a string. But __len is defined
> as a length operation, is it not?

Now what makes you think that length must be an integer?

$ lua -l complex
Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
> complex.__len = complex.abs
> #complex.new(3,4)
5.0

Reply | Threaded
Open this post in threaded view
|

Re: lpeg question

Dibyendu Majumdar
In reply to this post by Roberto Ierusalimschy
On 13 March 2018 at 20:46, Roberto Ierusalimschy <[hidden email]> wrote:

>> I don't interpret the manual that way. I read that as saying that you
>> cannot modify the length operator for a string. But __len is defined
>> as a length operation, is it not?
>
> I am not sure what you mean by "__len is defined as a length operation".
> It is *called* the length operator, but a name does not give its
> semantics. __add is also related to the "addition operation", but that
> name does not imply that it is an addition.
>
> Note the following:
>
> 1) The __eq metamethod, which always returns a boolean, says so:
>
>   "The result of the call is always converted to a boolean."
>
> 2) The API for the length operation does not return a number, but a
> Lua value. (The same is true for addition, but not for comparisons.)
>

Ok.

Thanks