-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revive boolean
type
#227
base: master
Are you sure you want to change the base?
Revive boolean
type
#227
Conversation
It should be discussed if local x:boolean
x = (function() return 1 end)()
print(x) should error, since local x:number
x = (function() return "1" end)()
print(x) does for strings. |
I think it shouldn't. It means that both But we may discuss implementation of explicit cast to
function toboolean(v)
if (v == 1 or v == '1' or v == 'true' ) then return true elseif (v == 0 or v == '0' or v == 'false') then return false end
end |
I implemented it in second commit. function test(i: integer) -- boolean
return @boolean(i)
end
local x:boolean
x = (function() return 1 end)()
print(x)
print(test(1))
But now I don't know, good it or bad, that |
** On successful conversion returns 1 and stores value in (*b). | ||
** On failure, returns 0. | ||
*/ | ||
int luaV_toboolean (const TValue *obj, int *b) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't comply with Lua semantics - string and numbers are true.
In general - boolean is a dubious type in Lua. It was added just to allow something like NIL value in a table. Roberto has said many times he regrets adding this type. I want to understand what is the benefit of adding this type? Is there a good use case? |
@@ -2589,6 +2616,13 @@ int luaV_execute (lua_State *L) { | |||
luaG_runerror(L, "string expected"); | |||
vmbreak; | |||
} | |||
vmcase(OP_RAVI_TOBOOLEAN) { | |||
int b; | |||
if (RAVI_LIKELY(luaV_toboolean(ra, &b))) { setbvalue(ra, b); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not convinced we should conversion.
(void)pc; | ||
emit_reg(fn, "ra", A); | ||
membuff_add_string(&fn->body, "int bool = 0;\n"); | ||
membuff_add_string(&fn->body, "if (luaV_toboolean(ra, &bool)) { setbvalue(ra, bool); }\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As above, not convinced conversion is a good idea.
Hello!
I'm here to resurrect the
boolean
type.Looks like it was dead a long time ago and buried in source code.
As for current Ravi version, there is zombie
boolean
type. Look:ravi: /home/ann/bool.ravi:0: type mismatch: expected boolean
Literally,
boolean
type exists, but it just does not work -- nor true nor false are "not boolean".After some necromancy,
boolean
shows signs of life.true boolean
src/ravi: /home/ann/bool.ravi:0: boolean expected
That works too:
src/ravi: b.ravi:4: Invalid assignment: boolean expected near <eof>
But I could forget to add it somewhere, please, double check it.