Skip to main content

Operator Methods

Val has many operator methods that help reduce the code needed to perform operations on Val object.

Using these operator methods also reduces the number of calls per operation, thus being more efficient than performing them manually (as seen in the Val equivalents).

These methods will not check if the operation is valid, so trying to do something devious like dividing a string will result in an error.

Assignment Operators

Assume a is a Val<T> and b is a T, where T is any type. If b were to be a Val<T>, then you would need to call b:get() inside each assignment call (e.g. a:add(b:get())).

NameOperatorVal MethodVal Equivalent
Additiona += ba:add(b)a:set(a:get() + b)
Subtractiona -= ba:sub(b)a:set(a:get() - b)
Multiplicationa *= ba:mul(b)a:set(a:get() * b)
Divisiona /= ba:div(b)a:set(a:get() / b)
Floor Divisiona //= ba:idiv(b)a:set(a:get() // b)
Modulusa %= ba:mod(b)a:set(a:get() % b)
Exponentiationa ^= ba:pow(b)a:set(a:get() ^ b)
Concatenationa ..= ba:cat(b)a:set(a:get() .. b)
Togglinga = not aa:flip()a:set(not a:get())

Additionally, you can also chain these methods together:

local x = Val.new(2)
x:pow(3):div(4):add(5):mul(6) -- (((x ^ 3) / 4) + 5) * 6
print(x:get()) -- 42

Math Operations

These methods behave the same as assignment operators.

NameOperationVal MethodVal Equivalent
Min Valuea = math.min(a, b, ...)a:min(b, ...)a:set(math.min(a:get(), b, ...))
Max Valuea = math.max(a, b, ...)a:max(b, ...)a:set(math.max(a:get(), b, ...))
Clampa = math.clamp(a, min, max)a:clamp(min, max)a:set(math.clamp(a:get(), min, max))
Absolute Valuea = math.abs(a)a:abs()a:set(math.abs(a:get()))
Floora = math.floor(a)a:floor()a:set(math.floor(a:get()))
Ceilinga = math.ceil(a)a:ceil()a:set(math.ceil(a:get()))
Rounda = math.round(a)a:round()a:set(math.round(a:get()))
Snapa = math.round(a / b) * ba:snap(b)a:set(math.round(a:get() / b) * b)
Lerpa = math.lerp(a, b, t)a:lerp(b, t)a:set(math.lerp(a:get(), b, t))

Relational Operators

Assume both a and b are a Val<T>, since these methods are meant to be used exclusively for comparing Val objects.

NameOperatorVal MethodVal Equivalent
Equal toa == ba:eq(b)a:get() == b:get()
Less thana < ba:lt(b)a:get() < b:get()
Less than or equal toa <= ba:le(b)a:get() <= b:get()
Greater thana > ba:gt(b)a:get() > b:get()
Greater than or equal toa >= ba:ge(b)a:get() >= b:get()