classLuauLuwu

Work 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
class.isinstanceLuauLuwu
Checks whether a value is an object of a class
class.implementsLuwuflagged
Checks whether a value's class implements a trait
class.ofLuwuflagged
Returns the class that built an object
class.nameLuwuflagged
Returns the name of a class, a trait or an object's class
class.fieldsLuwuflagged
Returns the public fields of an object, a class or a trait

class.isinstanceLuauLuwu

function class.isinstance(o: unknown, c: class): boolean

Returns 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.

Input
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))
Output
9
false
false

class.implementsLuwuflagged

function class.implements(o: unknown, t: trait): boolean

Returns 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.

Input
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()))
Output
something named Oreo
something without a name

class.ofLuwuflagged

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.

Input
class Account(public owner: string) end

const account = Account("Ada")
print(class.of(account) == Account)
print(class.of({}))
Output
true
nil

class.nameLuwuflagged

function class.name(o: class | object | trait): string

Returns the name of a class or trait, or the name of an object's class. Any other value raises an error.

Input
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))
Output
Cat
Cat
Named

class.fieldsLuwuflagged

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.

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.

Input
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)
Output
Ada
true
false