Skip to content

Computed Datatypes

Computed Datatypes are similar to light.literal() and light.enum(), except they are a function which returns a Datatype. They are mostly useful for LinkedList, and are best paired with Cached Datatypes.

Introducing external conditionality to serialization / deserialization can be dangerous. Please be careful.

You should never yield from within datatypes.computed()'s lambda callback(s).

This is considered undefined behavior.

You should never send any message from within datatypes.computed()'s lambda callback(s).

This is considered undefined behavior.

datatypes.computed() allows for recursive types.

If you pass a self-referential table, serialization may hang forever.

No implicit declarations

Returning implicitly defined Datatypes, such as Table Syntax, will not work. You must return an explicitly defined Datatype. To get around this, you can use datatypes.cached()

function light.datatypes.computed

Shared Experimental Synchronous
1
2
3
function computed<Output>(
    lambda: () -> (Datatype<Output>)
): (Datatype<Output>)

An example LinkedList Datatype using datatypes.computed() and Cached Datatypes:

linked_list.luau
local ty = light.datatypes

-- as a word of warning, you probably shouldn't give this a `head` field.
local function linkedlist<T>(value: Datatype<T>)
    local Datatype

    Datatype = ty.cached {
        next = ty.optional(ty.computed(function()
            return Datatype
        end)),

        value = value
    }

    return Datatype
end

return linkedlist