7.12 Type a collaborator with its class

Rule: §7.12 in the code standards; the existing loose cases are in B2.

Why

A parameter or field that always holds one script’s instance and is typed RefCounted, Object, Node or left untyped hides what it needs: a wrong argument parses, and the mistake surfaces as a missing-method error at run time, if the code path runs at all. Typed with the script, the same mistake is a parse error, and the signature names the dependency. RefCounted, Object, Node and untyped stay for values that really vary.

Reach the script through the aggregator (6.3) or, for a script the aggregator does not list, a direct preload. Keep the loose type, with a comment saying why, when the concrete type would:

  • add a preload cycle (6.6);
  • put an editor-only name in a runtime-reachable file’s preload closure (8.2);
  • need a new class_name (6.1);
  • change a signature an extension sees, such as MCPToolkitExtension.register or the registry API.

Good

The composer’s handle types the tracker it holds with the tracker’s script, reached through the aggregator (addons/godot_mcp_toolkit/core/plugin_composer.gd:210, :230):

const DebugSessionTracker = Modules.DebugSessionTracker
var _session_tracker: DebugSessionTracker = null  # The latest debugger session's state

The debugger bridge cannot be in the aggregator, because it preloads the aggregator (6.5). So the command module preloads it directly, says why, and types its parameters with it (addons/godot_mcp_toolkit/commands/debug_commands.gd:7-11):

# Modules cannot list it: it preloads Modules.
const DebugBridge := preload("res://addons/godot_mcp_toolkit/transport/debug_bridge.gd")


static func register(registry: MCPToolkitCommandRegistry, debug_bridge: DebugBridge) -> void:

The registrar that receives both from the composer types both (addons/godot_mcp_toolkit/transport/builtin_command_registration.gd:52-53):

static func register_all(registry: MCPToolkitCommandRegistry, server: Node,
		debug_bridge: DebugBridge, session_tracker: DebugSessionTracker) -> void:

Bad

The same bridge and tracker typed RefCounted, though only one script’s instance is ever passed (addons/godot_mcp_toolkit/commands/debug_commands.gd:9, register; addons/godot_mcp_toolkit/transport/builtin_command_registration.gd:48-49, register_all):

static func register(registry: MCPToolkitCommandRegistry, debug_bridge: RefCounted) -> void:
static func register_all(registry: MCPToolkitCommandRegistry, server: Node,
		debug_bridge: RefCounted, session_tracker: RefCounted) -> void:

Source

The maintainers’ decision, extending §4.1 (“type every function signature”) to the collaborator a signature receives. Background: Seemann, the service-locator anti-pattern (a hidden dependency turns a compile-time error into a run-time one): https://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern


This site uses Just the Docs, a documentation theme for Jekyll.