Skip to content

Cached

In light, you can use lua table syntax to represent a Datatype, and it'll be converted automatically when you use it in a Message.

This API exists to manually cache the result of a Datatype. This will internally turn it into a numeric ID if it isn't one already, rendering it unusable for anything other than ser/des and messages. Messages cache their Datatypes automatically, but this can be useful in conjunction with other features like Computed Datatypes.

function light.datatypes.cached

Shared Synchronous
1
2
3
function cached<T>(
   value: Datatype<T>
): (Datatype<T>)

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

linked_list.luau
local types = 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 = types.cached {
      next = types.optional(types.computed(function()
         return Datatype
      end)),

      value = value
   }

   return Datatype
end

return linkedlist

light.computed() allows for recursive types.

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