Well java objects get stringified, either by hashing or by an implementation, so if anything in the expression is not a number, its probably gonna be a string.
This sounds scary, but starts making a lot more sense when you consider that "+" is ALWAYS supposed to be addition in Lua. For string concatenation, ".." is used.
Been using Lua for a long time and I didn't even know that + was compatible with strings for this exact reason. It just isn't an issue you run into, unlike in JS
In Lua you can also do something like this for string concatenation, sorry I’m on phone so it will be hard to type ‘{not a string here} a string’ it works the same way just a little less messy I guess
I love it when scripting languages decide to make things "convenient" for new programmers who have never read a line of code in their life at the expense of everyone else.
Who decides what's reasonable? If I were to create a programming language, I'd allow the user to decide what's a reasonable way to cast (for example through operator overloading).
Lua was almost big in the game industry but it died because of the 1-indexing which made subtle off-by-1 incompatibility with all existing libraries, and made it impractical to directly bind libraries because they would have inconsistent indexing with Lua. The 1-indexing kind of destroyed the entire Lua project.
Lua is insanely popular for embedding in applications (e.g. scripting in games). I have no clue where you read about it being 'destroyed', but it is a really fast programming language (the Just-In-Time compiler is faster than Python or JS and interacts neatly with the C API). Used in World of Warcraft, Project Zomboid, Elder Scrolls Online, Roblox, Factorio, Don't Starve, and many others (from AAA studios as well). While the 1-indexing has a constant overhead due to memory access shenanigans, this is almost a non-issue.
I think this is actually part of the standard string library, it creates an __add metamethod for strings that converts them to numbers. Without it i believe this would just give "Attempted to perform arithmetic on a string value."
this is actually more of a consequence of using the wrong operator. String concatenation in Lua is purely just '..', not +. SO when u use the + operator on two objects, the Lua interpreter will see a string and try to covert to a number or fail and if its an object it will call the __add metamethod if defined or fail. I don't believe strings have meta methods because to my knowledge they are primitive types in Lua, not objects.
@@j3y445 i am aware of the concatenation operator, i aws simply explaining why this happens when you do it wrong. Strings do actually have a metatable, as each primitive type has a metatable shared among all instances of that type (so all strings share a metatable, all numbers, all bools, etc). This is why you can do this: local length = "hi everyone":len(). The string library adds all its functions to the string metatable alongside the "__add" function shown in hte video.
@@somenameidk5278 ah i see. And yea ur right about strings having a metatable. It’s been a while for me and lua. Though for numbers I really hope metamethods are only called for non-number things because otherwise there would be a pretty significant performance hit i would imagine. And is it the first operands metamethod that is actually called? I believe it should be and would make the most sense.
@@somenameidk5278while correct in the sense of pseudocoding - you’d need to make the string an object by wrapping it with parentheses or assigning it to a variable before you’re able to call the metamethods so itd be: `(“hi everyone”):len()`
@@j3y445 it tries the first operand first, and if it dosen't have that metamethod, it tries the second operand. i believe strings are the only type metatable that gets anything placed into it by the standard library.
The problem is that + is an arithmetic operation in lua, so lua will always try to do type coercion and you can even overload the operator to behave differently. For concatenation in Lua you always use *..* as it is the only method of joining two strings and with numbers it would cause an error.
Yes, but it should be error. You just can't add int to string, that's crazy. What it will do if you write 1 + "big", it will output prob something crazy
@@imaxx4 nope, first if you do 1 + "something that is not a number" it will error. But if you try to do an operation with a valid digit inside a string then it will transform the string into number and do the operation. I particularly like this, if the person entered a valid number it makes perfect sense for the language itself to deal with the conversion and addition. Now as you said 1 + "something" will error with "attempt to perform arithmetic on a string value"
@@hadawardgz I though it had something to do with ASCII char tables like in C. C reads characters as a number between 0-255, and then returns the ASCII character correspondent to that number when asked to. The ASCII code for the number 1 is 61. 61 + 1 = 62, and 62 in the ASCII is the number 2. So basically '1' + 1 = '2' There's a lot of weird things you can do in C, and this definetively isn't the weirdest.
@@guavapaste Hmm, but no. It's just that Lua actually performs coercion, it checks whether the string can be transformed into a number to operate on it.
since you can embed lua into c programs and supply your own mem alocator, I've managed to get lua running under 30k of ram, without the std library ofc, but still, kinda impressive. You can theoretically fit the whole lua interpreter into a NES RAM
@@Raspredval1337 if you defer the builtin libraries (table, os, io, string, math, etc) to not load until they are referenced (via metatables) then a new environment is less than 5k.
in this case i would say it isnt strictly useful but not problematic either. it kind of just is what it is. string concatenation using a different symbol from addition is a small quirk that's different from a lot of other programming languages, but, frankly, in my opinion, should be a standard in other programming languages. addition and string concatenation just should never have both been done with the same operator in any language. _that_ is actually problematic. it's not a huge, gigantic, big deal or anything, but it is a flaw in most other programming languages imo.
@@Templarfreak Concatenation isnt an Arithmatic operation so i see why they split it apart. Although other programming languages should certainly adapt to the way lua did it. Although people find JavaScript weird for this exact reason...
string concatenation differs from addition in a few ways that can be important: addition is commutative, concatenation is not addition is fast, concatenation is slow addition is invertible, concatenation is not addition distributes multiplication, concatenation does not (in python for example) the JIT could make use of these properties to simplify bytecode if not for ((dynamic typing and the operators being the same) or overriding not necessarily following that contract)
I started programming with C++, which, for most people, is a difficult language. I started programming in Python recently and it actually makes no sense lol. Its confusing how easy something can be
To be entirely fair, string concatenation in lua isn’t done with the + operator, it has a special .. operator Still hate that it tries to automatically cast a string to a number that’s horrible
I was for real searching for a comment that solves this and now that I found this, I feel so dumb, like I knew it existed, I even used it in the same project I'm using it rn, but I forgot it exists for some reason lol
@@Brahvim no if you write in JS 1+"1", you get 11, but if you write 1-"1", you get 0.... But in Lua in both cases you get sum of 1+1, and 1-1, so Lua in this much better.....
this is kind of the other way around, a '1' is actually just 49. c is rightly doing 1 + 49, and then %c is asking printf to take the number 50 and print it out in ASCII form
it's type coercion, it happen too in javascript 😂 type coercion is the automatic or implicit conversion of values from one data type to another (such as strings to numbers). type conversion is similar to type coercion because they both convert values from one data type to another with one key difference - type coercion is implicit whereas type conversion can be either implicit or explicit.
I thought implicit type conversion meant the computer just read the literal binary representation of a type, while explicit conversation actually converted it, no matter if it was specified in the code or not. You can do this in C, too--"printf("%c", 1 + '1');" will output "2", but "printf("%s", "12" + 1);" will . . . index into the string "12" and output "2", apparently. I thought I'd get a "you can't implicitly convert from pointer to int" error, but apparently C thought I wanted to do a little pointer arithmetic. You definitely can't implicitly convert from float to int without some union gymnastics, though. Do "printf("%c", 1.0 + "1");" and you get "invalid operands to binary +". C is a bit protective of floating point numbers.
@@mage3690 it depends on the language. Lua literally converts a string into a number, but will give you an error if you try to use addition with a string with ascii characters. but this is actually doable in something like C. in C you can do something like 1 + "a" and you'll get something like 98 i believe, but if you try to do that in lua it will complain at you because it cant convert "a" into a number via Lua's string to number function on the C side. however, because Lua lets you redefine any variable, including the inbuilt global variables, and any members of tables in those global variables, you can actually redefine the __add functions for the number and string metatables to handle the case yourself and make letters properly convert to their ascii representation and make 1 + "a" return 98 in Lua if you desire.
luau is trash cuz u can only learn lua inside of roblox. learning actual lua helps alot + there are better languages which are easier to learn like python
@@Fayroll5 why learning it inside roblox is trash? im 13 and i spent 2 years learning pro lua in roblox. Now im migrating to java, all thanks to roblox
I know that people couldn't care less about Lua these days but here is how you can legally disallow such behavior in your project. Watch the hands: getmetatable(" ").__add = nil Now what did I just do: when you try to "add" a string to a number, the string tries to do it in a naive way, fails (because strings aren't meant to be added by Lua), then looks into its metatable (an object that contains methods for values of certain type, basically Lua's way of OOP) and executes "__add" metamethod from there which does this wierd thing. So "getmetatable" function called with *any* string returns us that metatable, and then we wipe out "__add" metamethod from there. Now Lua cannot do anything when you add strings which makes it yield an error. It's not a hack or anything, Lua just doesn't mind exposing to you some things that are predefined by standard library Or just don't forget that there is ".." operator specifically for concatenation
@@CogTheSuit ??? I just checked it again I'm using just old plain Lua 5.4.6 interpreter from the official repository. not some unholy contraption used in Roblox or something. works in both REPL and chunk
I'm absolutely biased towards Python's handling of data structures. It becomes pretty recurrent to explicitly modify the class type (say, int("1") + 1), but I got used to it
@@GermanZindro When all you have is casting a number to a string for concatenation, then sure, it's not terrible. But JS is infamous for the way implicit type casting yield wildly undesirable results. For instance, `false == "0"` yields `true`, which you may never have guessed coming from any language with saner typing
I hope languages with type coercion adds a optional static type option to stop your suffering you made yourself... ...Implicit type casting is not good for large projects and can introduce bugs if its not detected. Thats why TypeScript exists for Javascript.
Since the + operator asserts both operands are of type number, IMHO it's explicit typecasting. PHP allows you to do strict typing by "declare(strict_types=1);" at the top of the file. Only JS has the literal worst of all worlds by (for some reason) allowing the computer to assume whatever the f*** it wants.
@xvutq706xcrt If you haven't understood the concept of watching the goddamn video, Python tells you that "hey, you are combining an integer and a string together so please fix that" because common sense, Lua on the other hand thinks it is an integer even though it's a string. But hey, you are a bot anyway.
yea lua's concatenation is exclusively the dots. so the + operator here implicitly tries to either cast the string to a number or invoke an object's __add metamethod.
Yeah I don't know if this entire comment section is trolling or everyone here is so knee deep into js/py brainrot they don't understand that + is not the concat operator. Yeah, turns out the math operator casts to numbers, just like the concat operator casts to strings, and people are calling this extremely intuitive system dumb?
every time i need to prove to my friends that python or lua is not any better i usually show them the weird syntax of those languages like whitespace for python and whatever this is for lua and they agree with me
You used the wrong operator if you are trying to concatenate. Concatentation in Lua is purely using .. and not +. So when u use +, you are trying to invoke a mathematical operation so the interpreter tries to cast the string to a number or fail, and if it's an object call its __add metamethod or fail. So ur + operator on a string a number will only fail if u have an invalid number. If you try to do print("a" + 1) it will fail, but if u do print("a" .. 1) it will succeed and print "a1". If you try to do print("1" .. 1) it will print 11. Yes its still bad because its an implicit behavior, but its also somewhat more sensical if u consider that Lua has actually a dedicated concatenation operator and + is purely a math operator.
@@mage3690 yea it’s explicit enough, but I sort of dislike the hidden control flow aspect of it. There’s a multitude of behaviors that can occur depending on context
@@mage3690i _think_ you have C to blame for this as it really set the standard for things like this afaik. but in C's case you can kind of forgive it because what C is doing really _is_ arithmetic addition and not some freaky type coercion because the C compiler doesnt _really_ see "a" as a string (well, in C's case a "char") it just sees it as any other pointer that happens to point to an index in the ascii table and 1 as an ascii character _happens_ to have an ascii code of 1. (well not "happens" as in by pure coincidence, it was specifically planned out and there is a lot of freaky manipulations you can do like turn any lower-case characters into its uppercase by adding some constant number to it) type coercion is sort of hidden but at the same time you would kind of expect it to correctly convert a string of "1" into a number of 1 and be properly able to add them together. this may seem bad on a type-safety standard but most average people would simply _expect_ this to work and originally back when Lua was first invented that was really who Lua was designed for. and imo this is a much better middleground than what something like Python or something like C do which are essentially two opposite ends of the spectrum. C is so oblivious to the fact that what you are trying to use is a "char" that it doesnt even consider it a possibility at all that your ascii character is meant to be a char and not an integer. meanwhile Python is so scared of its users doing something a little risky that it doesnt even let you do it at all without jumping through a hoop and basically completely invalidating any convenience you would get out of such a feature.
for anyone wondering, lua has a different concatenation symbol than python (being '..'). so when using + to attempt to concatenate a int or a float with a string containing an int or a float, it just results in the addition of both operands (otherwise resulting in an error if theres a type mismatch). if .. was used instead, it would output the intended result, being 11
yes. This video does not tell the full story, because Lua has a dedicated concatenation operator which is simply ".." . So by invoking the add operator (+) in this video, Lua will either try to convert a string to a number or fail, or invoke the __add metamethod for objects or fail. But by using the .. operator you will either concatenate a string and a number, or a string and an object by invoking the __concat metamethod or fail. So the control flow is pretty obvious, but only if you actually know lua.
if you have knowledge about basic concepts of programming, it'll take you a couple of days. Lua is basically Esperanto of programming languages, it is that easy to learn and almost as useless
@@sunofabeach9424 i like it because it’s quite nice as an embedded language for as much as extensibility or as little as just configuration. Anything else, it’s not very good for. It’s kind of a super powered json, toml, xml, etc.
python probably specifically does not allow you to output integer together with string, since you yourself determine what you output, integer or string
This might be one of the reasons why Roblox and CryEngine (its 2nd language) chose lua but not Python. At the same time, we use the string concatenation function and the addition function of companies such as Microsoft and Berry Labs with the "+" sign. However, Lua did not make this mistake and did the string concatenation with ".." and the addition with the "+" sign. As much as I wanted Roblox to use C# or JavaScript, we shouldn't underestimate Lua and frankly, I think it's a pretty good programming language. I would like to express my sincere congratulations to Roberto, Luiz and Waldemar!!
lua LUA.lua why you name the file like that bruh and why it's 2 auto conversion? it converts string to number to default usually because I think it's actually more practical than convert number to string because why?
in lua concatenating is done using the ".." operator, so if you do "+" it can only ever mean you are trying to perform an arithmetic addition, therefore doing int + string can only ever happen if you are actually trying to mathematically add them together
It's because of the purpose of the language. It's a client-side language. Usually you want a client application to run at least somehow no matter what happens. And Python is a server-side language where correctness is top priority
calculating numbers X string is not possible in some lua engines but you can do tonumber("1") to convert your current string into a number or just simply use 1 + 1 like a normal person 💀 (edit: btw some lua engines allow 1 + "1")
Lua if you want to make some active function you want variable exam local a = 1 local b = 2 local ab = 1 + 2 print(ab) But you can make short local a = 1 local b = 2 Print(a + b)
It's because lua autoamtically converts the string into a number. Imagine you have a user that inputs a number into the terminal and it returns a string, you would have to convert the string into a number manually wich works but it does it automatically so it's cool. And if it's a string that cant be converted into a number it will throw an error
@@mgames3209 he used the wrong operator. If you want concatenation, you have to use .. in Lua. + is always a mathematical operator in Lua. So it will either try to convert a string to a number or fail, or call an objects __add metamethod or fail. And if your intention is concatenation, with the .. operator, Lua will convert a number to a string or try to call __concat metamethod on objects or fail on either of those. And on failure it gives u a useful error message. It's still implicit and only Lua programmers will actually know what the heck is going on.
(Roblox Studio knowledge so my theory is prob wrong in the normal LUA cuz Roblox Studio uses a modified one I'm pretty sure) Problably sees that its trying to do an equation with a int value and a string value and converts the string to a number using tonumber()
I am a c programmer and am very confused, only briefly touched lua for nvim. Why is it not '2'? Take ascii code of '1', then add 1 to it and it becomes the ascii code for '2'? Alternatively this should work in most sane character sets aswell?
Assuming that Lua's type inference treats it as a String (char *), yes. Assuming that the type inference classifies it as a character then it'd be a '2' I believe in lua "" and '' are evaluated identically, assuming so, it'd be down to the type inference system which it choses. Alternatively it may use a Struct for strings which could be all kinds of a mess
Oh, I thought you were joking. "" in lua is neither a char or a char* - it's a lua string. How it is implemented doesn't matter, and you can't do arithmetic on it. The key to what happens here is the + operator, in lua, attempts to parse string operands into a number. 1+"1" in lua isn't equivalent to writing 1+"1" or 1+'1' in C, it's equivalent to 1+atoi("1").
if the intention was to concatenate, you have to use the .. operator in lua. + will always try to add mathematically through either a string conversion to number which can fail, or by invoking an object's __add metamethod, which can also fail. And upon failure, obviously there is a useful error that tells you. Its still an implicit behavior, but it makes sense if you know lua.
lmaoooooo, I actually made a post about that in my website, turns out that Lua is some years older than JavaScript, and has had string-to-number type coercion since it was first made when you use *any* operator, in fact, the string gets scanned to check if it's a number, and if it is, it's automatically converted to one
this makes an ok amount of sense since + only adds, and .. only concatenates, instead of js where + does both or python where you get an error. Yes yes tonumber() but I don’t care I am a lua supporter
It's fine in Lua for the most part (string concat and mathematical addition have different operators, so while the casting isn't explicit, it's not terrible)
who the fuck invited javascript devs to lua 💀🙏
fr
Fr
Francium
Javascript is inspired by lua
@@berthold64 And I thought that guy was haunted by visions of hell and decided to let it out all out.
I like Java's way of handling things: if there is a string on the middle, then everything is a String.
System.out.println() is beauty. The only problem is that it’s called System.out.println().
Well java objects get stringified, either by hashing or by an implementation, so if anything in the expression is not a number, its probably gonna be a string.
It’s just a lot easier
(1 + 2 + "3") is "33"
exactly
This sounds scary, but starts making a lot more sense when you consider that "+" is ALWAYS supposed to be addition in Lua. For string concatenation, ".." is used.
Been using Lua for a long time and I didn't even know that + was compatible with strings for this exact reason. It just isn't an issue you run into, unlike in JS
In Lua you can also do something like this for string concatenation, sorry I’m on phone so it will be hard to type
‘{not a string here} a string’
it works the same way just a little less messy I guess
@@goldenollies8338 string interpolation isn't part of vanilla Lua
@@CogTheSuit oh well it is with LOVE engine thing
@@CogTheSuit oh well it is with LOVE engine thing
So Lua doesn't stop you from shooting your leg. Very nice
Yeah I expected this to actually work and that that would be the issue.
Does it also work on JS?
@@pedroivog.s.6870 in JS 1 + "1" gives you "11", but 1 - "1" gives you 0
@@pedroivog.s.6870 almost certainly, that wretched language will type coerce nan to a string
@@pedroivog.s.6870 oh it's bad, really bad. JS doesn't and never will make sense. 1 + 1 + "1" = "21"
why people are dumb?
lua gave you “..” operator for concat and “+” for math 😒
so real
Legit it's is as logical as things can be
that's what i was thinking too
calm down dawg its a joke
I love it when scripting languages decide to make things "convenient" for new programmers who have never read a line of code in their life at the expense of everyone else.
string concatenation in lua is done with .. so theres really nothing else for + to do in that scenario, it might as well try to do some addition
So true
can't tell if this is sarcasm
It is sarcasm, @@phir9255.
@@jco_sfm it could be 11, but no, it's 2... but it's pretty useless either way lol
"static types are better"
"no! dynamic types are better!"
can't a man just get type inference with reasonable implicit casts?
Who decides what's reasonable? If I were to create a programming language, I'd allow the user to decide what's a reasonable way to cast (for example through operator overloading).
@@rikschaaf If you're not super picky you can easily find reasonable implicit cast systems.
It really isnt a hard problem.
Should be "a"+1 be "b" or "a1"?
Funny words magic man
learn rust or ocamel for that tbh, there are some great type inference in those
Lua was almost big in the game industry but it died because of the 1-indexing which made subtle off-by-1 incompatibility with all existing libraries, and made it impractical to directly bind libraries because they would have inconsistent indexing with Lua. The 1-indexing kind of destroyed the entire Lua project.
doesnt roblox use LUA? i wouldnt call exactly call it dead
well its not completely dead
Lua is insanely popular for embedding in applications (e.g. scripting in games). I have no clue where you read about it being 'destroyed', but it is a really fast programming language (the Just-In-Time compiler is faster than Python or JS and interacts neatly with the C API). Used in World of Warcraft, Project Zomboid, Elder Scrolls Online, Roblox, Factorio, Don't Starve, and many others (from AAA studios as well). While the 1-indexing has a constant overhead due to memory access shenanigans, this is almost a non-issue.
oh yeah? what about doom@@mudrost4934
I can’t even tell if this is a joke or not lmao. Saying something was almost good and then died because of 1 indexing is hilarious.
I think this is actually part of the standard string library, it creates an __add metamethod for strings that converts them to numbers. Without it i believe this would just give "Attempted to perform arithmetic on a string value."
this is actually more of a consequence of using the wrong operator. String concatenation in Lua is purely just '..', not +. SO when u use the + operator on two objects, the Lua interpreter will see a string and try to covert to a number or fail and if its an object it will call the __add metamethod if defined or fail. I don't believe strings have meta methods because to my knowledge they are primitive types in Lua, not objects.
@@j3y445 i am aware of the concatenation operator, i aws simply explaining why this happens when you do it wrong. Strings do actually have a metatable, as each primitive type has a metatable shared among all instances of that type (so all strings share a metatable, all numbers, all bools, etc). This is why you can do this: local length = "hi everyone":len(). The string library adds all its functions to the string metatable alongside the "__add" function shown in hte video.
@@somenameidk5278 ah i see. And yea ur right about strings having a metatable. It’s been a while for me and lua. Though for numbers I really hope metamethods are only called for non-number things because otherwise there would be a pretty significant performance hit i would imagine. And is it the first operands metamethod that is actually called? I believe it should be and would make the most sense.
@@somenameidk5278while correct in the sense of pseudocoding - you’d need to make the string an object by wrapping it with parentheses or assigning it to a variable before you’re able to call the metamethods so itd be: `(“hi everyone”):len()`
@@j3y445 it tries the first operand first, and if it dosen't have that metamethod, it tries the second operand. i believe strings are the only type metatable that gets anything placed into it by the standard library.
The problem is that + is an arithmetic operation in lua, so lua will always try to do type coercion and you can even overload the operator to behave differently. For concatenation in Lua you always use *..* as it is the only method of joining two strings and with numbers it would cause an error.
Yes, but it should be error. You just can't add int to string, that's crazy. What it will do if you write 1 + "big", it will output prob something crazy
@@imaxx4 nope, first if you do 1 + "something that is not a number" it will error. But if you try to do an operation with a valid digit inside a string then it will transform the string into number and do the operation. I particularly like this, if the person entered a valid number it makes perfect sense for the language itself to deal with the conversion and addition. Now as you said 1 + "something" will error with "attempt to perform arithmetic on a string value"
@@hadawardgz I though it had something to do with ASCII char tables like in C.
C reads characters as a number between 0-255, and then returns the ASCII character correspondent to that number when asked to. The ASCII code for the number 1 is 61. 61 + 1 = 62, and 62 in the ASCII is the number 2. So basically '1' + 1 = '2'
There's a lot of weird things you can do in C, and this definetively isn't the weirdest.
@@guavapaste Hmm, but no. It's just that Lua actually performs coercion, it checks whether the string can be transformed into a number to operate on it.
@@hadawardgz oh. That's cool.
Lua it just 150KB compiler while Python over a hundred megabytes does.
since you can embed lua into c programs and supply your own mem alocator, I've managed to get lua running under 30k of ram, without the std library ofc, but still, kinda impressive. You can theoretically fit the whole lua interpreter into a NES RAM
@@Raspredval1337 if you defer the builtin libraries (table, os, io, string, math, etc) to not load until they are referenced (via metatables) then a new environment is less than 5k.
@@Raspredval1337aw man NES developers shoulda added scripts to their games, imagine the possibilities 😢
@@niko5008If you wanna you could glitch the game to make it run arbitrary code. Look up snes code injection
Plus it's much faster.
lets be real, programming languages are cringe. i think we should send electrical impulses directly to the CPU to write binary code.
may as well break out your punch cards while youre at it
No you have to extract electricity from a jellyfish and input it to conputer to make efficient code to run
fr
@@turuukuruu ☝️🤓 jelly fishes produce venom not electricity nincompoop
Too abstract @@dijital4801
This type conversion is useful in some cases. But if you just wanna concatenate strings, doing ("1"+"1") wont actually work. This is why ".." exists.
in this case i would say it isnt strictly useful but not problematic either. it kind of just is what it is.
string concatenation using a different symbol from addition is a small quirk that's different from a lot of other programming languages, but, frankly, in my opinion, should be a standard in other programming languages. addition and string concatenation just should never have both been done with the same operator in any language. _that_ is actually problematic. it's not a huge, gigantic, big deal or anything, but it is a flaw in most other programming languages imo.
@@Templarfreak Concatenation isnt an Arithmatic operation so i see why they split it apart. Although other programming languages should certainly adapt to the way lua did it. Although people find JavaScript weird for this exact reason...
string concatenation differs from addition in a few ways that can be important:
addition is commutative, concatenation is not
addition is fast, concatenation is slow
addition is invertible, concatenation is not
addition distributes multiplication, concatenation does not (in python for example)
the JIT could make use of these properties to simplify bytecode if not for ((dynamic typing and the operators being the same) or overriding not necessarily following that contract)
then try print (0+"1"+"1")
@@xerlandia JavaScript says 011. Lua(u) says... 2
in JS it will be "11"
11 is honestly better than 2.
@@user-tw2kr6hg4r only if you use a 2 year old's definition of addition
@@user-tw2kr6hg4ronly if you arbitrarily make + also concatenation, which Lua does not do. + is only addition .. is concatenation
@@user-tw2kr6hg4r chill out terrence howard
In assembly it's 50
I started programming with C++, which, for most people, is a difficult language. I started programming in Python recently and it actually makes no sense lol. Its confusing how easy something can be
I did the opposite (started with python, now Java) and I want to kms
Started with python and now doing c. What is genuinely wrong with his language??? Why is it built like this
@@saharsh-sh Me with python be like
started with python, now learning c++@@saharsh-sh
It's because you just have started. Advanced python isn't easy.
To be entirely fair, string concatenation in lua isn’t done with the + operator, it has a special .. operator
Still hate that it tries to automatically cast a string to a number that’s horrible
I'm not sure but print(1 .. "1") or print(1, "1") should work.
I was for real searching for a comment that solves this and now that I found this, I feel so dumb, like I knew it existed, I even used it in the same project I'm using it rn, but I forgot it exists for some reason lol
To be honest... Is actually a good thing...
This is why i prefer to do everything in c/c++. It's either right or wrong, and i want to know which one it is. Not let the compiler figure it out.
tbh I expected it to be "11" like javascript
but lua has a separate sting concat operator (..)
What?
JavaScript does it THE SAME AS LUA.
@@Brahvim 1 + "1" === "11"
@@Brahvim no if you write in JS 1+"1", you get 11, but if you write 1-"1", you get 0.... But in Lua in both cases you get sum of 1+1, and 1-1, so Lua in this much better.....
@@Гдекамин Huh.
I've heard that numbers inside strings can sometimes be treated as parsed values in JS.
That's what I was referring to.
Javascript :- finally a worthy competitor
What about C?
printf("%c", 1 + '1') prints 2
man ascii
if you do 1 + '9' it will print ":" because it is after "9" in the ascii table
this is kind of the other way around, a '1' is actually just 49. c is rightly doing 1 + 49, and then %c is asking printf to take the number 50 and print it out in ASCII form
Chaotic neutral coder
@@thecwd8919 lmao
"You don't know me son"
~JavaScript
it's type coercion, it happen too in javascript 😂
type coercion is the automatic or implicit conversion of values from one data type to another (such as strings to numbers). type conversion is similar to type coercion because they both convert values from one data type to another with one key difference - type coercion is implicit whereas type conversion can be either implicit or explicit.
"if it happens in javascript, it shouldn't happen"
@@callyralb-but
antimatter dimensions :(
what will we spend hours watching without the ANTIMATTER?
I thought implicit type conversion meant the computer just read the literal binary representation of a type, while explicit conversation actually converted it, no matter if it was specified in the code or not. You can do this in C, too--"printf("%c", 1 + '1');" will output "2", but "printf("%s", "12" + 1);" will . . . index into the string "12" and output "2", apparently. I thought I'd get a "you can't implicitly convert from pointer to int" error, but apparently C thought I wanted to do a little pointer arithmetic.
You definitely can't implicitly convert from float to int without some union gymnastics, though. Do "printf("%c", 1.0 + "1");" and you get "invalid operands to binary +". C is a bit protective of floating point numbers.
@@mage3690 it depends on the language. Lua literally converts a string into a number, but will give you an error if you try to use addition with a string with ascii characters. but this is actually doable in something like C. in C you can do something like 1 + "a" and you'll get something like 98 i believe, but if you try to do that in lua it will complain at you because it cant convert "a" into a number via Lua's string to number function on the C side. however, because Lua lets you redefine any variable, including the inbuilt global variables, and any members of tables in those global variables, you can actually redefine the __add functions for the number and string metatables to handle the case yourself and make letters properly convert to their ascii representation and make 1 + "a" return 98 in Lua if you desire.
@@mage3690 That's because modern CPUs have dedicated hardware just for floats
By the way, i think Luau is more interesting as it's the roblox lua modified version that add typings to the language and make it more consistent
luau is trash cuz u can only learn lua inside of roblox. learning actual lua helps alot + there are better languages which are easier to learn like python
@@Fayroll5 there are luau interpreters out of roblox 🤷 actual Lua is cool, but if you want devs to be productive and efficient then Luau is better.
@@Fayroll5 why learning it inside roblox is trash? im 13 and i spent 2 years learning pro lua in roblox. Now im migrating to java, all thanks to roblox
pluto would actually be better. Luau is a mistake.@@hadawardgz
@@Fayroll5 you can download a standalone luau interpreter
when you play with the metatable too much
I know that people couldn't care less about Lua these days but here is how you can legally disallow such behavior in your project. Watch the hands:
getmetatable(" ").__add = nil
Now what did I just do: when you try to "add" a string to a number, the string tries to do it in a naive way, fails (because strings aren't meant to be added by Lua), then looks into its metatable (an object that contains methods for values of certain type, basically Lua's way of OOP) and executes "__add" metamethod from there which does this wierd thing. So "getmetatable" function called with *any* string returns us that metatable, and then we wipe out "__add" metamethod from there. Now Lua cannot do anything when you add strings which makes it yield an error.
It's not a hack or anything, Lua just doesn't mind exposing to you some things that are predefined by standard library
Or just don't forget that there is ".." operator specifically for concatenation
roblox uses lua though wdym "I know that people couldn't care less about Lua these days"
@@CuriousLight._dev no offense but I mean developers not Roblox players
Bad news for you, setting __add to nil doesn't give you anything
@@CogTheSuit ??? I just checked it again
I'm using just old plain Lua 5.4.6 interpreter from the official repository. not some unholy contraption used in Roblox or something. works in both REPL and chunk
@@sunofabeach9424 I used LuaJIT 2.1
Shit man, i don't know how i got here or why i stayed but damn my brain just farted
summary from the comments:
+ is arithmetic add
.. is concatenate
"+" casts the string to and int and thus prints 2
I'm absolutely biased towards Python's handling of data structures. It becomes pretty recurrent to explicitly modify the class type (say, int("1") + 1), but I got used to it
Explicit is better than implicit. The Zen of Python is generally correct on this sort of thing
Same, but for JS, just write 1+ +"1". Why waste time say lot words when few words do trick.
@@GermanZindro When all you have is casting a number to a string for concatenation, then sure, it's not terrible. But JS is infamous for the way implicit type casting yield wildly undesirable results. For instance, `false == "0"` yields `true`, which you may never have guessed coming from any language with saner typing
I hope languages with type coercion adds a optional static type option to stop your suffering you made yourself... ...Implicit type casting is not good for large projects and can introduce bugs if its not detected. Thats why TypeScript exists for Javascript.
TypeScript doesn't prevent the implicit type casting. you can string + number or vice versa without any errors.
Since the + operator asserts both operands are of type number, IMHO it's explicit typecasting. PHP allows you to do strict typing by "declare(strict_types=1);" at the top of the file. Only JS has the literal worst of all worlds by (for some reason) allowing the computer to assume whatever the f*** it wants.
add an optional*
it's*
that's*
there is a language server for Lua with strict static typing
@@klartnetNot if you annotate the variable as a number
Lua's way of printing that is even more cursed.
@xvutq706xcrt If you haven't understood the concept of watching the goddamn video, Python tells you that "hey, you are combining an integer and a string together so please fix that" because common sense, Lua on the other hand thinks it is an integer even though it's a string.
But hey, you are a bot anyway.
@__Merchant Lua doesn't think it is an integer, it takes it as an integer. For string concat, .. is used.
isn't lua string concatenation is double dots '..' or I just didn't know '+' also does concatenation?
yea lua's concatenation is exclusively the dots. so the + operator here implicitly tries to either cast the string to a number or invoke an object's __add metamethod.
Yeah I don't know if this entire comment section is trolling or everyone here is so knee deep into js/py brainrot they don't understand that + is not the concat operator.
Yeah, turns out the math operator casts to numbers, just like the concat operator casts to strings, and people are calling this extremely intuitive system dumb?
every time i need to prove to my friends that python or lua is not any better i usually show them the weird syntax of those languages like whitespace for python and whatever this is for lua and they agree with me
You used the wrong operator if you are trying to concatenate. Concatentation in Lua is purely using .. and not +. So when u use +, you are trying to invoke a mathematical operation so the interpreter tries to cast the string to a number or fail, and if it's an object call its __add metamethod or fail. So ur + operator on a string a number will only fail if u have an invalid number. If you try to do print("a" + 1) it will fail, but if u do print("a" .. 1) it will succeed and print "a1". If you try to do print("1" .. 1) it will print 11. Yes its still bad because its an implicit behavior, but its also somewhat more sensical if u consider that Lua has actually a dedicated concatenation operator and + is purely a math operator.
i think it was just a joke but yeah
Technically it's explicit behavior because the operator is asserting that both operands are of a certain type. At least, that's my opinion of it.
@@mage3690 yea it’s explicit enough, but I sort of dislike the hidden control flow aspect of it. There’s a multitude of behaviors that can occur depending on context
@@j3y445 yeah, it is hidden, and that's hella annoying. Especially when you can't change the hidden behavior, then it becomes downright infuriating.
@@mage3690i _think_ you have C to blame for this as it really set the standard for things like this afaik. but in C's case you can kind of forgive it because what C is doing really _is_ arithmetic addition and not some freaky type coercion because the C compiler doesnt _really_ see "a" as a string (well, in C's case a "char") it just sees it as any other pointer that happens to point to an index in the ascii table and 1 as an ascii character _happens_ to have an ascii code of 1. (well not "happens" as in by pure coincidence, it was specifically planned out and there is a lot of freaky manipulations you can do like turn any lower-case characters into its uppercase by adding some constant number to it)
type coercion is sort of hidden but at the same time you would kind of expect it to correctly convert a string of "1" into a number of 1 and be properly able to add them together. this may seem bad on a type-safety standard but most average people would simply _expect_ this to work and originally back when Lua was first invented that was really who Lua was designed for. and imo this is a much better middleground than what something like Python or something like C do which are essentially two opposite ends of the spectrum. C is so oblivious to the fact that what you are trying to use is a "char" that it doesnt even consider it a possibility at all that your ascii character is meant to be a char and not an integer. meanwhile Python is so scared of its users doing something a little risky that it doesnt even let you do it at all without jumping through a hoop and basically completely invalidating any convenience you would get out of such a feature.
for anyone wondering, lua has a different concatenation symbol than python (being '..'). so when using + to attempt to concatenate a int or a float with a string containing an int or a float, it just results in the addition of both operands (otherwise resulting in an error if theres a type mismatch). if .. was used instead, it would output the intended result, being 11
I have rewatched this too many times and I have died laughing each time
It actually makes more sense in Lua than JS because "+" is always addition.
lua LUA.lua
hole TWO.hole
@@emersoneisenlol I was about to say that
take TAKE take
Should I learn Lua? Seems like a fun time
yes. This video does not tell the full story, because Lua has a dedicated concatenation operator which is simply ".." . So by invoking the add operator (+) in this video, Lua will either try to convert a string to a number or fail, or invoke the __add metamethod for objects or fail. But by using the .. operator you will either concatenate a string and a number, or a string and an object by invoking the __concat metamethod or fail. So the control flow is pretty obvious, but only if you actually know lua.
Lua is actually really good, the haters here are overreacting and saying subjective stuff.
if you have knowledge about basic concepts of programming, it'll take you a couple of days. Lua is basically Esperanto of programming languages, it is that easy to learn
and almost as useless
@@sunofabeach9424 i like it because it’s quite nice as an embedded language for as much as extensibility or as little as just configuration. Anything else, it’s not very good for. It’s kind of a super powered json, toml, xml, etc.
there is no greater pleasure in life than using lua tables
1 + "1",
character for "1" = 49
1 + 49 = 50 which is character for 2
I remember learning lua and one of my early mistakes was using ‘+’ instead of ‘..’ to concatenate and being so confused.
I was always very curious about Lua, after seeing this I want to learn it
In roblox studio cause roblox script use lua. When you do that 1 + "1" the console will scream at you and say are you trying to do ..
i’ve only just stuck my fingers into the pie that is javascript, so naturally i have to be on my boy lua’s side
python probably specifically does not allow you to output integer together with string, since you yourself determine what you output, integer or string
Python beginner here, I can’t feel my brain watching this
This might be one of the reasons why Roblox and CryEngine (its 2nd language) chose lua but not Python. At the same time, we use the string concatenation function and the addition function of companies such as Microsoft and Berry Labs with the "+" sign. However, Lua did not make this mistake and did the string concatenation with ".." and the addition with the "+" sign. As much as I wanted Roblox to use C# or JavaScript, we shouldn't underestimate Lua and frankly, I think it's a pretty good programming language. I would like to express my sincere congratulations to Roberto, Luiz and Waldemar!!
this is why '..' exists for concat
lua LUA.lua
why you name the file like that bruh
and why it's 2 auto conversion? it converts string to number to default usually because I think it's actually more practical than convert number to string because why?
this is known as type coercion, where using specific operators changes the type
1 + "1" -> 2 (number)
"5"..5 -> "55" (string)
I think it's a must be programmed crime towards performance
Are we just going to ignore all those lines before the print statement in Lua? 😂
If you allow integer to plus string unintendtionally and no error raised, it's bug in your software you might never ever gonna find it.
in lua concatenating is done using the ".." operator, so if you do "+" it can only ever mean you are trying to perform an arithmetic addition, therefore doing int + string can only ever happen if you are actually trying to mathematically add them together
It's because of the purpose of the language. It's a client-side language. Usually you want a client application to run at least somehow no matter what happens. And Python is a server-side language where correctness is top priority
calculating numbers X string is not possible in some lua engines but you can do tonumber("1") to convert your current string into a number or just simply use 1 + 1 like a normal person 💀
(edit: btw some lua engines allow 1 + "1")
Lua if you want to make some active function you want variable exam
local a = 1
local b = 2
local ab = 1 + 2
print(ab)
But you can make short
local a = 1
local b = 2
Print(a + b)
It's because lua autoamtically converts the string into a number. Imagine you have a user that inputs a number into the terminal and it returns a string, you would have to convert the string into a number manually wich works but it does it automatically so it's cool. And if it's a string that cant be converted into a number it will throw an error
JavaScript: "11"
At least it’s better then 2.
@@mgames3209 he used the wrong operator. If you want concatenation, you have to use .. in Lua. + is always a mathematical operator in Lua. So it will either try to convert a string to a number or fail, or call an objects __add metamethod or fail. And if your intention is concatenation, with the .. operator, Lua will convert a number to a string or try to call __concat metamethod on objects or fail on either of those. And on failure it gives u a useful error message. It's still implicit and only Lua programmers will actually know what the heck is going on.
@@j3y445oh ok
"Attempt to perform arithmetic (add) on number and string"
(Roblox Studio knowledge so my theory is prob wrong in the normal LUA cuz Roblox Studio uses a modified one I'm pretty sure) Problably sees that its trying to do an equation with a int value and a string value and converts the string to a number using tonumber()
You can also do this in JavaScript. But I have a genuine question. Why would you ever wanna do this?
tf was all that yap at the beginning of the lua file?
I don't know what's going on, that's why I'm sticking to batch files.
bro added an int and a str together 💀
b-but you tried to add a string to a number so of course it would output ERROR and not 2
~_~
t-that's the joke...
I am a c programmer and am very confused, only briefly touched lua for nvim. Why is it not '2'? Take ascii code of '1', then add 1 to it and it becomes the ascii code for '2'? Alternatively this should work in most sane character sets aswell?
I am a C programmer as well and I'm pretty sure it should be an empty string.
Assuming that Lua's type inference treats it as a String (char *), yes.
Assuming that the type inference classifies it as a character then it'd be a '2'
I believe in lua "" and '' are evaluated identically, assuming so, it'd be down to the type inference system which it choses.
Alternatively it may use a Struct for strings which could be all kinds of a mess
Oh, I thought you were joking.
"" in lua is neither a char or a char* - it's a lua string. How it is implemented doesn't matter, and you can't do arithmetic on it.
The key to what happens here is the + operator, in lua, attempts to parse string operands into a number. 1+"1" in lua isn't equivalent to writing 1+"1" or 1+'1' in C, it's equivalent to 1+atoi("1").
in lua, it would display "11" which is a string
As a lua dev, my day is ruined
i fucking love this man. Lua is strong 💪
Lua: BROTHER! Look at me!!
Javascript: ...
wiat doesn't lua concatenate strings with double dots...?
Automatic conversion by convenience, but any competent developer would already know that...
When I make a mistake I just want the language to call me an idiot to my face, not think it behind my back while nodding
im questioning my life rn with this
As Python geek, I can only say syntax error on line 42
oh no, dont forget to cast your numbers to string before generating strings
if the intention was to concatenate, you have to use the .. operator in lua. + will always try to add mathematically through either a string conversion to number which can fail, or by invoking an object's __add metamethod, which can also fail. And upon failure, obviously there is a useful error that tells you. Its still an implicit behavior, but it makes sense if you know lua.
im pretty sure this is because of strings that only have numbers are treated as an int
lmaoooooo, I actually made a post about that in my website, turns out that Lua is some years older than JavaScript, and has had string-to-number type coercion since it was first made
when you use *any* operator, in fact, the string gets scanned to check if it's a number, and if it is, it's automatically converted to one
Performance issues everywhere
print(1.. "1")
esolang where its just a completely normal language except you have to spell "concatenate" whenever you wanna concatenate 2 strings
if u want it to print 1 + "1" then just do print("1 + "1"")
LUA is the aimware of programming languages
Hey what are datatypes never heard of it
THIS IS ILLEGAL NOOOOOOOOOOOOOOOOOO AS A PYTHON USER IM CRYING
print("1+1")
this makes an ok amount of sense since + only adds, and .. only concatenates, instead of js where + does both or python where you get an error. Yes yes tonumber() but I don’t care I am a lua supporter
Javascript: 2 + "2" = 22; 2 - "2" = 0
Lua:
Lua is peak (no joke involved (yes, really no joke (really (fr ( ok im fr ) ) ) ) ) )
The correct way to type a math operation in LUA is: print(1 +1 )
Actually this: print("1" .. 1)
will work to without type error (returns 22).
That's quite nice)
what debugger do you use for lua?
print "some string" is syntactically valid, btw, :3
tragically-typed programming language
Bruh maybe because first variable is a number and second is the string? And if you would do it like this print(1 + 1) it will work
this video save my life. thank you.
I dont see how this could ever cause hidden issues
It's fine in Lua for the most part (string concat and mathematical addition have different operators, so while the casting isn't explicit, it's not terrible)
print (1 + 1) output: 2, print ("yo wsg my g") output: yo wsg my g
That's why I like lua. It just makes more sense. Like how does "+" means concatenation!?!?
Bro the music 😂
1 + "1" = ERROR , about left parameter number, and right parameter text