Unexpected result in Lua test code

Asked

Viewed 122 times

5

I wrote a little code to test a statement from the book I’m studying, Programming in Lua (3rd Edition).

The statement, which is found on page 10 of the book, translated is as follows: Conditional tests (e.g., conditions in control structures) consider both the boolean value false and nil as false and everything else like true.

function test (a)
    if a then print(type(a).." '"..tostring(a).."'".." corresponde a verdadeiro")
    else print(type(a).." '"..tostring(a).."'".." corresponde a falso") end
end

print("Teste 1: ")
test(true)

print("\nTeste 2: ")
test(0)

print("\nTeste 3: ")
test("")

print("\nTeste 4: ")
test("a")

print("\nTeste 5: ")
test(test)

print("\nTeste 6: ")
test(test("masuia"))

print("\nTeste 7: ")
test(false)

print("\nTeste 8: ")
test(nil)

print("\nTeste 9: ")
test(a) -- variável 'a' não foi inicializada, então corresponde a nil

When I went to run the code, I received the following result on the console:

Teste 1: 
boolean 'true' corresponde a verdadeiro

Teste 2: 
number '0' corresponde a verdadeiro

Teste 3: 
string '' corresponde a verdadeiro

Teste 4: 
string 'a' corresponde a verdadeiro

Teste 5: 
function 'function: 000000000026F2A0' corresponde a verdadeiro

Teste 6: 
string 'masuia' corresponde a verdadeiro
nil 'nil' corresponde a falso

Teste 7: 
boolean 'false' corresponde a falso

Teste 8: 
nil 'nil' corresponde a falso

Teste 9: 
nil 'nil' corresponde a falso

Why does the "Test 6" result occur? According to the book’s statement the test was not to have false result, since the parameter passed was not the boolean value false nor nil? Besides, where did that second line of the result come from?

1 answer

7


It’s totally consistent. You noticed that this step 6 makes two tests?

First he executes test("masuia") and so the first line is printed. By then you should be finding everything right.

Then it runs the test again by calling the function result test("masuia"). What is this result, IE, what is the value returned? None, ie, nil. So the second call in the background is running test(nil), and the result is right. Looking the other way that expression could be seen this way, just so you understand better:

temp = test("masuia") -- vai resultar em nil já que a função sem retorna tem valor nil
print(tmp) -- só para você ver o resultado intermediário.
test(tmp)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I don’t know if you’ve learned how expressions work, how the call and return functions work.

Now you may be wondering what is the difference between the function call test() within the function call itself test() and from example 5 where he calls test(test). The first has a call syntax, ie the function test is executed at that time. Parentheses say this. In the second case you are just passing the value contained in the identifier test, which happens to be a function. So it works as a variable and does not call the function. We can say that he is "passing the function" as argument, which is quite different from executing the function and passing its result as argument, as is the case of example 6.

It doesn’t pass all the code, but something that indicates where the code is (I won’t go into detail, it’s too early for you to learn about it). This indicator is that weird number that appears on test 5, which is a memory address.

  • I already have a certain understanding of functions and expressions (as well as pointers) thanks to my studies of other languages, but did not know that the Lua functions returned nil when no return was specified. Great answer and very well explained!

  • Many languages do this, especially the dynamic ones, where you need to have value for everything to give more flexibility.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.