classAdded by Luau 0.722.
LuauAdded by Luwu: class.implements, class.of, class.name, class.fields
Changed by Luwu: class.isinstance
Luwu
class.implements, class.of, class.name, class.fieldsclass.isinstanceWork in progress
This page is not up to our standards yet.
This can be because is not complete or hasn't been reviewed and copyedited properly. Some details may be poorly worded, wrong, or unclear. Please let me (deviaze) know on Discord if you need clarification on anything.
The class library inspects classes and objects at runtime. It checks whether a value is an instance of a class or implements a trait, and reads which class made an object and a class's name and fields.
Classes and objects are primitives in Luwu: type() returns "class" for a class and "object" for an object. class.isinstance and class.implements narrow the value they check inside an if.
Summary
function class.isinstance(o: unknown, c: class): booleanclass.isinstanceAdded by Luau 0.722.
LuauChanged by Luwu: Available without a debug flag.
Luwu- Checks whether a value is an object of a class
function class.implements(o: unknown, t: trait): booleanclass.implementsAdded by Luwu.
LuwuAvailable in luwu/integration and gated by an FFlag. We anticipate this feature to be released and enabled by default in 0.1.0.
Needs the
flaggedLuwuTraitsFFlag.- Checks whether a value's class implements a trait
function class.of(o: unknown): class?class.ofAdded by Luwu.
LuwuAvailable in luwu/integration and gated by an FFlag. We anticipate this feature to be released and enabled by default in 0.1.0.
Needs the
flaggedLuwuClassesFFlag.- Returns the class that built an object
function class.name(o: class | object | trait): stringclass.nameAdded by Luwu.
LuwuAvailable in luwu/integration and gated by an FFlag. We anticipate this feature to be released and enabled by default in 0.1.0.
Needs the
flaggedLuwuClassesFFlag.- Returns the name of a class, a trait or an object's class
function class.fields(o: class | object | trait): ({ [string]: unknown }, boolean)class.fieldsAdded by Luwu.
LuwuAvailable in luwu/integration and gated by an FFlag. We anticipate this feature to be released and enabled by default in 0.1.0.
Needs the
flaggedLuwuClassesFFlag.- Returns the public fields of an object, a class or a trait
class.isinstanceAdded by Luau 0.722.
LuauChanged by Luwu: Available without a debug flag.
Luwu
function class.isinstance(o: unknown, c: class): booleanReturns true when o is an object of the class c. Any value that isn't an object gives false.
An object belongs to exactly one class, and Luwu has no is-a relationship between classes. Use class.implements to ask whether an object's class implements a trait.
Inside an if or after an assert that calls it, the checker treats o as an object of c.
class Circle(public radius: number) end
class Square(public side: number) end
const function area(shape: Circle | Square): number
if class.isinstance(shape, Circle) then
return math.pi * shape.radius ^ 2
end
return shape.side ^ 2
end
print(area(Square(3)))
print(class.isinstance(Circle(1), Square))
print(class.isinstance("circle", Circle))9 false false
class.implementsAdded by Luwu.
LuwuAvailable in luwu/integration and gated by an FFlag. We anticipate this feature to be released and enabled by default in 0.1.0.
Needs the LuwuTraits FFlag.
flagged
LuwuTraits FFlag.function class.implements(o: unknown, t: trait): booleanReturns true when o is an object whose class implements the trait t. The class can list t after implements or get it through another trait that needs it. Any value that isn't an object gives false.
Passing a class as t raises an error that points you to class.isinstance. Inside an if that calls it, the checker treats o as an object that implements t.
trait Named
expect name: string
end
class Cat(public name: string) implements Named end
class Rock end
const function describe(thing: unknown): string
if class.implements(thing, Named) then
return `something named {thing.name}`
end
return "something without a name"
end
print(describe(Cat("Oreo")))
print(describe(Rock()))something named Oreo something without a name
class.ofAdded by Luwu.
LuwuAvailable in luwu/integration and gated by an FFlag. We anticipate this feature to be released and enabled by default in 0.1.0.
Needs the LuwuClasses FFlag.
flagged
LuwuClasses FFlag.function class.of(o: unknown): class?Returns the class that built the object o, or nil when o isn't an object.
A Luau module (a .luau file) running with upstream class compatibility calls this function by upstream's name, class.classof. Luwu modules don't see that name.
class Account(public owner: string) end
const account = Account("Ada")
print(class.of(account) == Account)
print(class.of({}))true nil
class.nameAdded by Luwu.
LuwuAvailable in luwu/integration and gated by an FFlag. We anticipate this feature to be released and enabled by default in 0.1.0.
Needs the LuwuClasses FFlag.
flagged
LuwuClasses FFlag.function class.name(o: class | object | trait): stringReturns the name of a class or trait, or the name of an object's class. Any other value raises an error.
trait Named
expect name: string
end
class Cat(public name: string) implements Named end
print(class.name(Cat))
print(class.name(Cat("Oreo")))
print(class.name(Named))Cat Cat Named
class.fieldsAdded by Luwu.
LuwuAvailable in luwu/integration and gated by an FFlag. We anticipate this feature to be released and enabled by default in 0.1.0.
Needs the LuwuClasses FFlag.
flagged
LuwuClasses FFlag.function class.fields(o: class | object | trait): ({ [string]: unknown }, boolean)Returns a table keyed by the names of the public fields of an object, a class or a trait. The second result is true when that table holds every field and false when the class has private fields it left out.
- For an object, each field maps to its current value. A field holding
nilshows up asnoneso its name stays in the table. - For a class or trait there are no values yet, and every field maps to
none.
When the checker knows the class, it types the table with that class's public fields. A field of type string? comes back as string | none.
class Profile(
public name: string,
public nickname: string? = nil,
private password: string = ""
) end
const fields, complete = class.fields(Profile("Ada"))
print(fields.name)
print(fields.nickname == none)
print(complete)Ada true false