This is a follow up question relating to my question on sub modules:
Discourse question on submodules
Okay. My issues seems to be around circular dependencies. I have an engine that wants to call methods that some “other” code defined outside of the engine. The problem is that that other code also needs to reference “things” defined in the engine. For example, I have an AbstractNode defined in the engine and so my client (aka the game) needs to reference it. The engine—when it is running— will then call these “generic” methods.
The issue is I don’t want the engine “hard coded” on the game methods because that would negate the generality of the engine. The engine should server any game.
Any ideas on how to solve this?
I put together a simple demonstration of attempting to do such a thing. The code is also at Repl.it code.
Naturally I get the error:
ERROR: LoadError: UndefVarError: transition not defined
Stacktrace:
[1] getproperty(::Module, ::Symbol) at ./sysimg.jl:13
[2] top-level scope at none:0
[3] include at ./boot.jl:326 [inlined]
[4] include_relative(::Module, ::String) at ./loading.jl:1038
[5] include(::Module, ::String) at ./sysimg.jl:29
[6] include(::String) at ./client.jl:403
[7] top-level scope at none:0
in expression starting at /home/runner/main.jl:44
Example code:
# Game is the module I want to augment with a new
# function called `transition()` in which the engine will call
# But I can't define transition yet because AbstractNode isn't
# defined yet, hence the chicken/egg conundrum.
module Game end
module Ranger
module Nodes
using ...Game
# Simple demo node for example ----------
abstract type AbstractNode end
mutable struct Node <: AbstractNode
x::Float64
end
crossnode = Node(1.0)
# -------------------------------------
function visit()
Game.transition(crossnode)
end
end
end
# Demo engine
module Engine
using ..Ranger.Nodes:
visit
function run()
visit()
end
end
# ------------------
# Attempt to extend Game module
using .Ranger.Nodes:
AbstractNode
using .Game
# Attemping to add `new` function to Game module.
function Game.transition(node::AbstractNode) # <-- UndefVarError: transition not defined
println("game transition")
end
# -------------------------
using .Engine:
run
function go()
run()
end
go()
Thanks