#Any way to notate the type of self in OOP automatically?

1 messages · Page 1 of 1 (latest)

undone kelp
#

I have two code examples.
example a:

--!strict

local object = {}
object.__index = object    

local self : object

function object.new(Number : number?) : object
    local self = setmetatable({},object)
    
    self.var = Number
    
    return self
end

function object:doThing()
    self.var = 500
end

type object = typeof(object.new())
return object

and example b:

--!strict

local object = {}
object.__index = object    

local self : object

function object.new(Number : number?) : object
    local self = setmetatable({},object)
    
    self.var = Number
    
    return self
end

function object:doThing()
    local self : object = self
    
    self.var = 500
end

type object = typeof(object.new())
return object

The only difference is that in class methods and specifically class methods, to get the convenience of knowing what is stored in self, I notated that self is an object and it is equal to self.

Why is this a big deal? it's not. I just really like not having to constantly go back to check what variables and methods and whatnot are and are not in objects of a class. You'll see this yourself if you plug in the code to a module and type "self.", in which it'll bring up all the items stored in self. But that behavior is not extended to the scope of class methods.

So I was wondering if there was anything I could do to get said convenience of the type notation without having to write that one line at the start of every method. If you don't know or it's literally impossible, that's fine. It's really no big deal.

#

look at the bottom🥀

limber kelp
meager plank
#

tl;dr no, luau typesolver cannot infer self

meager plank
#

and yes you absolutely can do that

#

;compile lua lua local t={f="foo"} t.doThing=function(self) print(self.f) end t:doThing()

loud lodgeBOT
#
Program Output
foo

limber kelp
heady tapir
#

I recommend not doing metatables for objects if you plan on doing type

Instead use a regular table to create the class and type that, then use a deep copy util to copy the class.

I personally use https://sleitnick.github.io/RbxUtil/api/TableUtil/ this table util to do all my deep copy, i also use reconcile if i have a starting table that i want to be loaded into the class

so like i have a classSchema type that has something like

export type shipScheme = {
    Cargo: {},
    Upgrades: {},
    Speed: number,
    Crew: number,
    Health: number,
    Cannons: number,
}

type ShipClass = {
    ShipModel: Model,
    ShipId: string,
    SpawnShip: (self: ShipClass, location: CFrame) -> (),
    ReplicateTo: (self: ShipClass, location: CFrame) -> ()
} & shipScheme 

export type ShipObject = ShipClass

-- // MAIN
local ShipClass: ShipClass = {} :: ShipClass

function ShipClass:ReplicateTo(location:CFrame)
    self.ShipModel:PivotTo(location)
end

function ShipClass:SpawnShip(location: CFrame)
    
    CharacterUtil.PivotCharacterToPosition(self.ShipModel, location)
    self.ShipModel.Parent = game.Workspace
end

function Ship.CreateShip(scheme: shipScheme): ShipObject
return TableUtil.reconcile(scheme, ShipClass)
end

and this is how i make objects and classes work with luaU without metaTables. it allows for easy type casting without confusing yourself or the IDE

A collection of helpful table utility functions. Many of these functions are carried over from JavaScript or
Python that are not present in Lua.

Tables that only work specifically with arrays or dictionaries are marked as such in the documentation.

:::info Immutability
All functions (except SwapRemove, SwapRemoveFirstValue, and Lock) t...

undone kelp
#
  1. what does the :: in local ShipClass: ShipClass = {} :: ShipClass do?
heady tapir
#

Its called type casting

#

Since i fill out that table later on in the code, the IDE will yell at me because {} does not match the ShipClass type

So i say, hey ignore what {} looks like, this is what i want you to think it looks like

heady tapir