lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Wed, 25 May 2022 at 11:03, Duke Normandin <> wrote:
My mistake! However I set myName = 'blah' which is a string!
Correct? I can then say myName = 'blah-blau' and no error message
shows up.
I'm missing something!

be mindful about the difference between values and variables.

myName is a variable.  it can hold any value, and can be changed to any other value
"blah" is a string.  it won't change.  it will always be "blah"

when you change myName, it changes the variable, not the value.

that sounds silly, but think of two variables with the same string.  if you change one, now each variable points to a different string.  because the original value wasn't changed.   it behaves like numbers.  you wouldn't expect "3" to change!

it's different from objects like tables, for example.  think two variables pointing to the same table.  you modify one, and they're still the same table, so the change is seen from both variables.

in language terms, this is referred as "value semantics" (like numbers) and "object semantics" (like tables, objects and other containers).  in some languages (like C), strings are "objects" (or rather, arrays of chars), so they can be modified even if shred.  in many higher level languages, strings are immutable so have "value semantics".  it also helps when using strings as keys, for example.