Lua - Error in execution of return and or









up vote
1
down vote

favorite












I am basically doing some testing to get to know the Lua language a bit better. I found a bug that makes zero sense to me.



Function:



local function d(c)
return (!c and print("c", false) or print("c", true))
end

local function a(b, c)
return (!b and d(c) or print("b", true))
end


When i run a(1, nil) or a(1, 1) it outputs b true, but if i run a(nil, 1) then it outputs c true and b true



If anyone could enlighten me on why it returns two value when that technically shouldn't be possible?










share|improve this question

















  • 1




    You are confusing return values with printed output. On top of that you can return multiple values in Lua.
    – Henri Menke
    Nov 9 at 22:03










  • I know that it is possible to return multiple values in lua, but for this case it should choose either, not both. But thanks for explaining why i experienced this bug.
    – Lukas Knudsen
    Nov 9 at 23:07














up vote
1
down vote

favorite












I am basically doing some testing to get to know the Lua language a bit better. I found a bug that makes zero sense to me.



Function:



local function d(c)
return (!c and print("c", false) or print("c", true))
end

local function a(b, c)
return (!b and d(c) or print("b", true))
end


When i run a(1, nil) or a(1, 1) it outputs b true, but if i run a(nil, 1) then it outputs c true and b true



If anyone could enlighten me on why it returns two value when that technically shouldn't be possible?










share|improve this question

















  • 1




    You are confusing return values with printed output. On top of that you can return multiple values in Lua.
    – Henri Menke
    Nov 9 at 22:03










  • I know that it is possible to return multiple values in lua, but for this case it should choose either, not both. But thanks for explaining why i experienced this bug.
    – Lukas Knudsen
    Nov 9 at 23:07












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am basically doing some testing to get to know the Lua language a bit better. I found a bug that makes zero sense to me.



Function:



local function d(c)
return (!c and print("c", false) or print("c", true))
end

local function a(b, c)
return (!b and d(c) or print("b", true))
end


When i run a(1, nil) or a(1, 1) it outputs b true, but if i run a(nil, 1) then it outputs c true and b true



If anyone could enlighten me on why it returns two value when that technically shouldn't be possible?










share|improve this question













I am basically doing some testing to get to know the Lua language a bit better. I found a bug that makes zero sense to me.



Function:



local function d(c)
return (!c and print("c", false) or print("c", true))
end

local function a(b, c)
return (!b and d(c) or print("b", true))
end


When i run a(1, nil) or a(1, 1) it outputs b true, but if i run a(nil, 1) then it outputs c true and b true



If anyone could enlighten me on why it returns two value when that technically shouldn't be possible?







debugging error-handling lua






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 21:30









Lukas Knudsen

288




288







  • 1




    You are confusing return values with printed output. On top of that you can return multiple values in Lua.
    – Henri Menke
    Nov 9 at 22:03










  • I know that it is possible to return multiple values in lua, but for this case it should choose either, not both. But thanks for explaining why i experienced this bug.
    – Lukas Knudsen
    Nov 9 at 23:07












  • 1




    You are confusing return values with printed output. On top of that you can return multiple values in Lua.
    – Henri Menke
    Nov 9 at 22:03










  • I know that it is possible to return multiple values in lua, but for this case it should choose either, not both. But thanks for explaining why i experienced this bug.
    – Lukas Knudsen
    Nov 9 at 23:07







1




1




You are confusing return values with printed output. On top of that you can return multiple values in Lua.
– Henri Menke
Nov 9 at 22:03




You are confusing return values with printed output. On top of that you can return multiple values in Lua.
– Henri Menke
Nov 9 at 22:03












I know that it is possible to return multiple values in lua, but for this case it should choose either, not both. But thanks for explaining why i experienced this bug.
– Lukas Knudsen
Nov 9 at 23:07




I know that it is possible to return multiple values in lua, but for this case it should choose either, not both. But thanks for explaining why i experienced this bug.
– Lukas Knudsen
Nov 9 at 23:07












1 Answer
1






active

oldest

votes

















up vote
2
down vote













Maybe you already understand what's happening, but I've already written this post. Lua doesn't have a ! operator; I guess you meant not. (I wouldn't be surprised if someone has made a patched version of Lua with ! in place of not.)



a(nil, 1) returns not nil and d(1) or print("b", true). Now, not nil evaluates to true and d(1) evaluates to nil, so we have true and nil or print("b", true), which in turn evaluates to nil or print("b", true), and therefore print("b", true) is evaluated.



As to why d(1) evaluates to nil: it returns not 1 and print("c", false) or print("c", true). That is equivalent to not 1 and nil or nil because print always returns nothing when it is called, and nothing is treated as nil by the operators and and or. not x and nil or nil always evaluates to nil whether x is truthy or falsy, so d always returns nil. (The only difference is that if d receives a falsy value, both print calls are evaluated.)



You can verify that print returns nothing by calling type(print('a')): it throws the error "bad argument #1 to 'type' (value expected)", whereas type(nil) returns "nil".






share|improve this answer






















  • Probably OP uses GarrysMod
    – Egor Skriptunoff
    Nov 10 at 11:31










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233510%2flua-error-in-execution-of-return-and-or%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













Maybe you already understand what's happening, but I've already written this post. Lua doesn't have a ! operator; I guess you meant not. (I wouldn't be surprised if someone has made a patched version of Lua with ! in place of not.)



a(nil, 1) returns not nil and d(1) or print("b", true). Now, not nil evaluates to true and d(1) evaluates to nil, so we have true and nil or print("b", true), which in turn evaluates to nil or print("b", true), and therefore print("b", true) is evaluated.



As to why d(1) evaluates to nil: it returns not 1 and print("c", false) or print("c", true). That is equivalent to not 1 and nil or nil because print always returns nothing when it is called, and nothing is treated as nil by the operators and and or. not x and nil or nil always evaluates to nil whether x is truthy or falsy, so d always returns nil. (The only difference is that if d receives a falsy value, both print calls are evaluated.)



You can verify that print returns nothing by calling type(print('a')): it throws the error "bad argument #1 to 'type' (value expected)", whereas type(nil) returns "nil".






share|improve this answer






















  • Probably OP uses GarrysMod
    – Egor Skriptunoff
    Nov 10 at 11:31














up vote
2
down vote













Maybe you already understand what's happening, but I've already written this post. Lua doesn't have a ! operator; I guess you meant not. (I wouldn't be surprised if someone has made a patched version of Lua with ! in place of not.)



a(nil, 1) returns not nil and d(1) or print("b", true). Now, not nil evaluates to true and d(1) evaluates to nil, so we have true and nil or print("b", true), which in turn evaluates to nil or print("b", true), and therefore print("b", true) is evaluated.



As to why d(1) evaluates to nil: it returns not 1 and print("c", false) or print("c", true). That is equivalent to not 1 and nil or nil because print always returns nothing when it is called, and nothing is treated as nil by the operators and and or. not x and nil or nil always evaluates to nil whether x is truthy or falsy, so d always returns nil. (The only difference is that if d receives a falsy value, both print calls are evaluated.)



You can verify that print returns nothing by calling type(print('a')): it throws the error "bad argument #1 to 'type' (value expected)", whereas type(nil) returns "nil".






share|improve this answer






















  • Probably OP uses GarrysMod
    – Egor Skriptunoff
    Nov 10 at 11:31












up vote
2
down vote










up vote
2
down vote









Maybe you already understand what's happening, but I've already written this post. Lua doesn't have a ! operator; I guess you meant not. (I wouldn't be surprised if someone has made a patched version of Lua with ! in place of not.)



a(nil, 1) returns not nil and d(1) or print("b", true). Now, not nil evaluates to true and d(1) evaluates to nil, so we have true and nil or print("b", true), which in turn evaluates to nil or print("b", true), and therefore print("b", true) is evaluated.



As to why d(1) evaluates to nil: it returns not 1 and print("c", false) or print("c", true). That is equivalent to not 1 and nil or nil because print always returns nothing when it is called, and nothing is treated as nil by the operators and and or. not x and nil or nil always evaluates to nil whether x is truthy or falsy, so d always returns nil. (The only difference is that if d receives a falsy value, both print calls are evaluated.)



You can verify that print returns nothing by calling type(print('a')): it throws the error "bad argument #1 to 'type' (value expected)", whereas type(nil) returns "nil".






share|improve this answer














Maybe you already understand what's happening, but I've already written this post. Lua doesn't have a ! operator; I guess you meant not. (I wouldn't be surprised if someone has made a patched version of Lua with ! in place of not.)



a(nil, 1) returns not nil and d(1) or print("b", true). Now, not nil evaluates to true and d(1) evaluates to nil, so we have true and nil or print("b", true), which in turn evaluates to nil or print("b", true), and therefore print("b", true) is evaluated.



As to why d(1) evaluates to nil: it returns not 1 and print("c", false) or print("c", true). That is equivalent to not 1 and nil or nil because print always returns nothing when it is called, and nothing is treated as nil by the operators and and or. not x and nil or nil always evaluates to nil whether x is truthy or falsy, so d always returns nil. (The only difference is that if d receives a falsy value, both print calls are evaluated.)



You can verify that print returns nothing by calling type(print('a')): it throws the error "bad argument #1 to 'type' (value expected)", whereas type(nil) returns "nil".







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 3:20

























answered Nov 9 at 23:16









cyclaminist

64919




64919











  • Probably OP uses GarrysMod
    – Egor Skriptunoff
    Nov 10 at 11:31
















  • Probably OP uses GarrysMod
    – Egor Skriptunoff
    Nov 10 at 11:31















Probably OP uses GarrysMod
– Egor Skriptunoff
Nov 10 at 11:31




Probably OP uses GarrysMod
– Egor Skriptunoff
Nov 10 at 11:31

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233510%2flua-error-in-execution-of-return-and-or%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus