lloyAlloy on GitHub
Extensions
Docs

Extensions

Extension methods on foreign types

Updated

impl Vector3 or impl string adds methods to a type you do not own. The server injects each method into the analyzer's definitions. The call types, completes, and hovers like a built-in.

Alloyextensions.aly
1export impl Vector3
2    function flat(self): Vector3
3        return Vector3.new(self.X, 0, self.Z)
4    end
5end
6local f = pos:flat()
Emitted Luausame line count
1local __alloy = require("@alloy") do local __impl = __alloy.impl_for("Vector3")
2    function __impl.flat(self): Vector3
3        return Vector3.new(self.X, 0, self.Z)
4    end
5end
6local f = __alloy.call(pos, "flat") return nil
  • Project wide. The build and the server collect every extension first, so a call from another file dispatches too.
  • Primitives such as string go through a typed helper table, since a definitions file cannot add a method to string values.
  • The check artifact keeps pos:flat() as written and types self; the ship artifact routes through the dispatcher, which calls the extension first. A method of the type itself with the same name runs only when no extension names it.