Unexpected result in Lua test code

5

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

The statement on page 10 of the translated text is as follows: Conditional tests (for example, conditions in control structures) consider both the Boolean value false and nil as false and everything more 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 ran 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 statement of the book, the test was not supposed to be false, since the last parameter was not the boolean value false nor nil ? Also, where did this second row come from?

    
asked by anonymous 04.04.2015 / 00:30

1 answer

6

It's totally consistent. Did you notice that this step 6 does two tests?

First it runs test("masuia") and so the first line is printed. By then you must be finding everything right.

Then it re-runs the test calling the result of the test("masuia") function. What is this result, that is, what is the returned value? None, that is, nil . So the second call in the background is running test(nil) , and the result is right. Looking otherwise that expression could be seen this way, just so you can 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)

See running on ideone .

I do not know if you have already learned how expressions work, how call functions and function returns.

You should now be wondering what the difference between calling the test() function within the call itself is from the test() function and from example 5 where it calls test(test) . The first one has a call syntax, that is, the test function is executed at that time. Relatives 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 it is "passing the function" as an argument, which is quite different from executing the function and passing its result as an argument, as is the case in example 6.

It does not pass all the code, but rather something that indicates where the code is (I will not go into details, it's early for you to learn about this). This indicator is the odd number that appears in Test 5, which is a memory address.

    
04.04.2015 / 00:49