# var=time() vs time()=var pico8/lua

time() is a preset function in pico8 gameengine.

I don't understand whatsoever why I get an error when I write: time() = var

But it's fine if I write (where var is any variable) var = time()

What are the rules dictating order? This makes no sense to me.

For some context consider a time function:

time_diff=0

function time_change()
if time() - time_diff > 1 then
time_diff=time()
end
end


if You change to time()=time_diff.....it generates an error.

• What is the error that is given to you? – Vaillancourt Jul 21 at 11:36
• syntax error near = – kite Jul 21 at 11:38
• Are you not allowed to set a function equal to a variable, but allowed to set a variable equal to a function? you get the same kinda thing when doing x=io.read() vs io.read()=x. I woulda thought = signs would imply both ways rather than 1 way. – kite Jul 21 at 11:40

In math, the = sign represents the equality comparison operator, an assertion that "the thing on the left has the same value as the thing on the right."

This operator is commutative: you can exchange the left & right sides without changing anything (the statement that the left & right sides have the same value remains as true or as false as it was before. Reading them in a different order doesn't change its truth value)

In programming languages like Lua, the equality comparison operator is written ==, with two equals signs.

In these languages, when you write just one =, you're not doing an equality comparison at all. This is a completely different operation: an assignment operator.

The assignment operator = says "take the value of the thing on the right, and assign it to the variable on the left"

It's the difference between "check whether these things are equal" (==) versus "make these two things equal" (=)

But of course there are two ways we could make the things equal by changing the value of one side: the left or the right. So, we say that the assignment operator always changes the left side. That means that assignment is not commutative. Observe:

local a = 1
local b = 2

if aFirstThenBSecond then
a = b
else
b = a
end

• If we take the first branch, we're saying "take the value of b and store it in variable a," so both variables hold the value 2 when this code finishes executing.

• If we take the second branch, we're saying "take the value of a and store it in variable b," so both variables hold the value 1 when this code finishes executing.

In both cases, a and b are equal to one another at the end, so the meaning of = as "make these things equal" holds both ways.

We get a different resulting value in the variables depending on the order of our assignment, because we're asking the computer to take a meaningfully different action each way.

That's why...

• myVar = time() is valid: it says "take the current time value and store it in the variable myVar

• time() = myVar is not valid: it says "take the value of myVar and store it into the current time" which doesn't make sense.

You could argue that programming language authors should have chosen a different symbol/syntax for assignment, like destination <-- source, but it turns out we need to write assignment operations a lot when we create programs, so having it be short and accessible without a shift key is very useful.

Some languages will let you type just a single = for both equality comparison and assignment, and the compiler/interpreter will try to figure out which one you wanted based on the surrounding context. This can lead to bugs though if the programmer thought they were comparing but the compiler read it as an assignment, which is why Lua and C-like languages use different operators for them. Some will also warn or error if you write something like this:

if myVar = 5 then...


Which looks like a comparison ("take this branch if and only if myVar has a value of 5") but is actually an assignment ("set myVar to 5 and then take this branch"). So some programmers will program defensively by using the fact that flipping an assignment with a constant/function call is an error, to make a what's called a "Yoda Condition":

if 5 == myVar then...


If we remember to use the == then this comparison works right in either order. If we accidentally forget one = and end up writing an assignment, it's an error "you can't assign a new value to the number 5!" which helps us find and fix our typo instead of letting the bug linger unfound. ;)

I'm not familiar with the languge.

time() being a function and returning a value, it's generally not possible (in most languages) to assing a value to such returned values (and I really wonder what would be the intention).

If you find it possible to do the same with other language native functions (I have not been able to find anything about io.read()), it is most likely because the function returns a reference, and it is to that that you are assinging a new value (something that tells where the actual value is).

I don't know any language where you can interchange the order of operands and you get the same effect, it's generally destination = source.