#How to resolve Cyclic module dependency?

1 messages · Page 1 of 1 (latest)

prisma temple
#

State and StateMachine dont depend on each other via code but they do depend on each other for types this causes a cyclic dependency since they both need to require each other to get the states. I've tried to make a type module but that doesn't seem to work no matter how hard I try. Plus if use typeof it says the code is too complex to type check.

Should I just drop type checking in OOP it just doesnt seem like its worth the hassle ngl. 😭

spice mesa
#

move them both to a single modulescript for both types

prisma temple
#

so merge state and statemachine into one module?

spice mesa
#

something like that ya, for the types

prisma temple
#

is it not possible to keep them separate though just so its not all crammed in one module?

#

cause for some reason using typeof() to get the type for self and the metatable is treated as a different type to merging state_self and state_class.

brave sand
#

I had this exact same issue a few weeks ago

#

State manager, types, cyclic dependency

#

are you me in a different universe?

prisma temple
#

this thing is beating the crap out of me cause its the first time ive really tried to type check my stuff

spice mesa
#

the types do not need to be in the same script as the implementation

prisma temple
spice mesa
#

and frankly, seeing __index in there, your types are not correct anyway since this will not match the type of the metatable

prisma temple
#

cause if I do that then the type checker says your code is too complex to check thats why im trying to use intersection

spice mesa
#

type x = setmetatable<a,b>

#

or type x = typeof(setmetatable(...))

#

depends which solver you use

prisma temple
#

yea so like export type State = typeof(setmetatable({} :: State_self,{} :: StateClass))?

spice mesa
#

for the old solver ya that's where you'd start

#

👍

prisma temple
#

ok and should I just ignore when it says code too complex to type check?

spice mesa
#

up to you idk what the complexity is 🤷

prisma temple
#

hmm ok and see how im using the StateMachine_Class to sub for the actual stateMachine methods is that fine? cause u said that __index makes it wrong?

spice mesa
#

i didnt say it made it wrong, i said it tells me that you're making it a metatable

#

and if you're doing that then the types wont match what the data actually is

prisma temple
#

yea you need to use typeof there right?

#

since in the error it says it expects "this type" but got "this type" and they both look the same

#

its just annoying since if I use metatables for the type it then complains and wont type check it since its too complex

prisma temple
swift gate
#
local typeExample = {}

export type typeExample.Type = {[string]: boolean}
export type typeExample.BooleanType = boolean

return typeExample
#

smth like that should work

#

🤔

prisma temple
# swift gate wdym ur type module didn't work? How did u make it?

Basically I got two modules StateMachine and State:

StateMachine - needs State type for its root since root is a State
State - needs StateMachine type so it can refer to it

problem is it means they both require each other so it creates the circular dependency. Ive tried to make a dedicated shared types module however im struggling with that. This is what my shared type module looks like:

prisma temple
swift gate
#

then before you define each type

#

add types.

#

before it

#

e.g.

#
export type types.StateMachine = ...
#

then have the module returns types

prisma temple
swift gate
#

what..?

#

im confused

#

what are you saying?

prisma temple
#

so you want me to define each type before I add the types?

#

im confused

swift gate
#

ok

#

listen

#

change your export type

#

to

#
export type types.YourTypeName = ...(whatever the type ='s)
prisma temple
#

ohh I see yea

swift gate
#

make sure u have a variable called types

#

the whole point is to return types so the module has access to all types

#

thats the reason your type module wasn't working

prisma temple
swift gate
#

send a ss if it doesn't work

prisma temple
#

so I put Types. before defining each type? I think I did something wrong

swift gate
#

oh, thats NOT what I thought u meant

#

mb sob2k

swift gate
# prisma temple so I put Types. before defining each type? I think I did something wrong
--!strict

local Types = {}

export type State_Self = {
    stateMachine: StateMachine,
    parent: State?,
    name: string,
    childStates: {State},
    activeChild: State?,
}

export type State_Class = {
    new: (stateMachine: StateMachine, parent: State?, name: string) -> State,

    GetInitialState: (self: State) -> State?,
    GetTransition: (self: State) -> State?,
    OnEnter: (self: State) -> (),
    OnExit: (self: State) -> (),
    OnUpdate: (self: State, deltaTime: number) -> (),

    Enter: (self: State) -> (),
    Exit: (self: State) -> (),
    Update: (self: State, deltaTime: number) -> (),

    GetLeaf: (self: State) -> State,
    PathToRoot: (self: State) -> {State},
}

export type State = State_Self & State_Class

return Types

#

try this

#
local typeModule = require(whatever.your.path.is.typeModule)

local function exampleFunction(state: typeModule.State)
  ...
end
#

and that should work

prisma temple
#

im finally free

prisma temple
swift gate
#

type modules aren't meant to use __index

prisma temple
swift gate
#

why would you use OOP for a type module??

woeful oysterBOT
#

studio** You are now Level 7! **studio

prisma temple
swift gate
#

no?

prisma temple
#

yea this is my state module so I was tryna type check this

#

thats why I thought u had to include __index in the type

swift gate
#

do you understand how OOP works?

prisma temple
#

yea id say well enough im just a dumbass with types

#

like I wouldnt call myself an expert but I know how it works good enough id say

#

and on some dev forums they included __index in the type definition so I assumed u had to include it

swift gate
#

sideeye'

#

yknow what

#

you will learn as you go ig

prisma temple
#

yea

swift gate
#

do you at least have a state manager?

prisma temple
#

thanks anyway you have actually been a great help ur the goat

prisma temple
swift gate
#

wat

prisma temple
#

yea I think I know enough to get by

prisma temple
# swift gate wat

Im just tryna learn hfsm and im just messing around with the code to see how it works

woeful oysterBOT
#

studio** You are now Level 11! **studio

prisma temple
#

and just tryna learn the types as I go

swift gate
#

oh

#

gl