Relational operators evaluation?

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

Relational operators evaluation?

Bill Kelsoe
In Chapter 3 concerning relational operators expressions, PIL says that "if values have different types, Lua considers them not equal"

So if I understood this, this means that if a boolean and a string are related, using ~= , means that they are like apples and oranges?

Because right after that statement, "Otherwise, Lua compares them according to their types."

So if a value is Boolean meaning either true or false, and the other comparator is a String, of the value "true", so they are evaluated equal by Lua even if ~= is used?

hope I understood it correctly.
Reply | Threaded
Open this post in threaded view
|

Re: Relational operators evaluation?

Javier Guerra Giraldez
On 24 April 2017 at 17:06, Bill Kelsoe <[hidden email]> wrote:
> In Chapter 3 concerning relational operators expressions, PIL says that "if
> values have different types, Lua considers them not equal"

that means that any value is different of any value of a different
type.  p.ej any string is different to any number

> So if I understood this, this means that if a boolean and a string are
> related, using ~= , means that they are like apples and oranges?

yes.  (except that i don't see what you mean by "related", I guess the
word was "compared")


> Because right after that statement, "Otherwise, Lua compares them according
> to their types."

"Otherwise", means "if the previous sentence doesn't apply".  the
previous condition was "values have different types", so here the
condition is "both values have the same type".  then, the comparison
is according to that type.


> So if a value is Boolean meaning either true or false, and the other
> comparator is a String, of the value "true", so they are evaluated equal by
> Lua even if ~= is used?

no; a boolean true is of a different type of a string "true", so
they're different.



--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: Relational operators evaluation?

Andrew Starks-2
In reply to this post by Bill Kelsoe

On Mon, Apr 24, 2017 at 11:07 Bill Kelsoe <[hidden email]> wrote:
In Chapter 3 concerning relational operators expressions, PIL says that "if values have different types, Lua considers them not equal"

So if I understood this, this means that if a boolean and a string are related, using ~= , means that they are like apples and oranges?

Because right after that statement, "Otherwise, Lua compares them according to their types."

So if a value is Boolean meaning either true or false, and the other comparator is a String, of the value "true", so they are evaluated equal by Lua even if ~= is used?

hope I understood it correctly.

No. 

If two values are of different types (one is a table and one is not a table), then they will not be equal. They will not be equally, even if the table has a metatable with the metamethod:

My.__eq = function() return true end

In part, this is why the paragraph exists: to explain the limitation of the __eq metamethod, which is only called when both types are equal. 

Consider a complex number library that produces a value that can hold both complex and non-complex numbers: 

c = complex.new(3) --returns an object that represents a complex number. 
if c == 3 then 
  print "will execute."
else
  print "will execute."
end

This is because Lua will never check for the existence of an __eq metamethod because the type of the value of "c" is "table" (or userdata, depending on how the library is implemented) and the value of the literal "3" is "number."

Inequality is treated the same way. 


Reply | Threaded
Open this post in threaded view
|

Re: Relational operators evaluation?

Frank Kastenholz-2
In reply to this post by Bill Kelsoe
Hi
You're teaching yourself how to program, asking questions as needed, etc ... This is all good.

Others are answering your question directly, so I'll be different

One of the lessons that rarely, if ever, appears in any text is "Code it up and try it out".
Don't worry, you won't break anything
And you will learn more about writing software 
You also will learn exactly what Lua does ... Not what the manual says it will do or what other programmers say it will do (neither of which are infallible ... Though in this case, I expect they both are right)


 Monday, April 24, 2017, Bill Kelsoe [hidden email] wrote:
In Chapter 3 concerning relational operators expressions, PIL says that "if values have different types, Lua considers them not equal"

So if I understood this, this means that if a boolean and a string are related, using ~= , means that they are like apples and oranges?

Because right after that statement, "Otherwise, Lua compares them according to their types."

So if a value is Boolean meaning either true or false, and the other comparator is a String, of the value "true", so they are evaluated equal by Lua even if ~= is used?

hope I understood it correctly.
Reply | Threaded
Open this post in threaded view
|

Re: Relational operators evaluation?

Daurnimator
On 25 April 2017 at 03:22,  <[hidden email]> wrote:

> Hi
> You're teaching yourself how to program, asking questions as needed, etc ...
> This is all good.
>
> Others are answering your question directly, so I'll be different
>
> One of the lessons that rarely, if ever, appears in any text is "Code it up
> and try it out".
> Don't worry, you won't break anything
> And you will learn more about writing software
> You also will learn exactly what Lua does ... Not what the manual says it
> will do or what other programmers say it will do (neither of which are
> infallible ... Though in this case, I expect they both are right)

To a degree.

If you read something in the manual and aren't sure how to apply it,
then sure: give it a try in the interpreter and see how you go.

However, basing all your information on what you see when you try it
out is going to end up relying on implementation defined or platform
specific details.

I think the questions being asked are good; and if anything, show our
lack of good introductory texts.
I'm hesitant to recommend the online PiL lately as it's age is
starting to show; asking intrigued newcomers to purchase a book is
also not reasonable in my opinion.