A Mock Undefined Value

About

As Luau does not currently have an undefined value, I took it upon myself to create a mock undefined value that mirrors the undefined of JS to the best of Luau’s ability (using metatables). Refer to Demo for a demonstration.

Demo

Code:

local undefined = require(path.to.undefined)

local testTable = {
	year = "1997",
	brand = "Ford",
	driver = undefined.new(),
	wheelBrand = nil
}

print(testTable)

if not undefined.test(testTable.driver) then
	print("Assigning new driver!")
end

Result:
image

Usage

undefined.new(): [metatable Undefined]
This returns a metatable that has custom error messages for practically every metamethod. It has a single key value of DEBUG_CHECK that exists for the undefined.test method. Undefined does not equal nil and it takes up space in tables.

undefined.test(objA: any, objB: any): boolean
Since directly comparing the return value of undefined.new() to nil returns false, this test method exists. If both objA and objB are the result of undefined.new(), this will return true. Otherwise, it returns false. If only objA exists, it returns false, as a single undefined value should be falsey and not nil.

Caveat: Calling type() or typeof() on this custom value will return table.

Links

Roblox Module: Undefined - Roblox
GitHub Gist: Undefined value for Luau · GitHub

1 Like

is it that hard to just do

local cope = {
   seethe = "undefined"
   malde = true
}

or act like a normal person

1 Like

Read up on this.

Quite simply, it’s better practice. It gives an option to make a space in a table that isn’t really saying “undefined” as a string or isn’t really saying “I don’t exist” but instead it’s saying “I don’t exist yet

Your example is the reason why I made this module. It is a placeholder that means “I don’t exist yet”. You should try to understand what undefined actually is before posting.

2 Likes

Which makes sense in your example. Driver should be nil because they dont exist yet when the car is created

Either way this isnt good practice. Its a waste that nil easily beats and replaces.

1 Like

Let’s say you have made a table of an user’s data in your game, let’s call it x

x contains many information about the user, and it is required to listed out in a list. If we assign nil to one of the column in the data, that entire column would be erased out, which means when being listed out, that column would not exist in the list. As a result, a mock dummy value would be a greater alternative when in this situation.

Even though you can use something else that represents “this data does not exist yet”, but can be problematic. Here’s how:

Okay, now, this time, instead of using nil, you decided to use a string “undef”, which stands for undefined. The column still exists, but is written with “undef”. We don’t know what that data represents, could it be actual input from the user, or actually represents “undefined”, it’s creating a lot of edgecases that we generally want to avoid, not resolve.

A mock dummy value does not take up a lot of space, simply create a new mt with newproxy can already do the job.

1 Like

Which saves space and makes it easier to view. You don’t want blank info to be displayed anyway.

It’s not that hard to block “undefined” as a user input lol

1 Like

In that case, why not create a table? {"undefined"}. Or even just a blank table {}. That seems to be what your module is doing, more or less.

1 Like