You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A default parameter value is checked without trait subsumption, so a concrete type cannot be the default for a trait-typed parameter — even though the identical value is accepted when passed explicitly.
trait FileStore do
readFile : String -> String?
end
record RealFS do end
make RealFS, implement: FileStore do
let readFile(p: String) -> String? = None
end
let a(p: String, fs: RealFS = RealFS {}) -> String = "ok" # fine
let b(p: String, fs: FileStore = RealFS {}) -> String = "ok" # error
error: Type mismatch: expected FileStore, got RealFS
Subsumption works everywhere else. Passing the value explicitly is accepted:
let stamp(c: Clock) -> String = "at ${c.now()}"
stamp(FakeClock { t: 7 }) # fine — dispatches correctly
and so is an annotated binding:
let r: Clock = RealClock {} # fine
Only the default-value position uses strict type equality.
Why it matters
This is the shape that makes explicit dependency injection usable: the dependency is invisible at production call sites and supplied only in tests.
Without it, every caller must pass the dependency explicitly, which is why the pattern has not been adopted. Related to #143, though independent of the capability design proposed there.
The cause was not subsumption — argMatchesParam already handles a trait-named parameter correctly. It is ordering, and typechecker.cxx:436 says so outright:
We do two passes: first check all FunctionDefs in sorted order, then check everything else (types, records, make blocks, main).
Every function is checked before any make block, so while a default value was being validated no local trait conformance existed yet and m_traits.satisfies(RealFS, "FileStore") returned 0. That also explains the two confusing near-misses: a builtin trait (Showable) worked because builtins are registered upfront, and a default that was a bound variable "worked" only because it infers as Unknown, which the check skips.
Fix is registerLocalConformances, a pass that registers the declared (type, trait) pairs right after registerTraits and before any function body is checked. checkTraitImplementation still runs later and still reports a make block that fails to provide the required methods — only the declaration of intent moves earlier.
A second, separate fix was needed for BEAM. Lowering passes a trait dictionary derived from the call-site argument, so an omitted defaulted argument had none:
error: IR lower: missing source argument for FileStore dictionary
src/ir/lower.cxx now takes the dictionary from the default expression when the argument is absent, at both dictionary sites. Without it this fix was walker-only.
Spec: spec/trait_default_param.kex, verified identical on both backends.
Activity
akoskovacs commentedon Aug 22, 2026
Fixed on both backends.
The cause was not subsumption —
argMatchesParamalready handles a trait-named parameter correctly. It is ordering, andtypechecker.cxx:436says so outright:Every function is checked before any
makeblock, so while a default value was being validated no local trait conformance existed yet andm_traits.satisfies(RealFS, "FileStore")returned 0. That also explains the two confusing near-misses: a builtin trait (Showable) worked because builtins are registered upfront, and a default that was a bound variable "worked" only because it infers asUnknown, which the check skips.Fix is
registerLocalConformances, a pass that registers the declared(type, trait)pairs right afterregisterTraitsand before any function body is checked.checkTraitImplementationstill runs later and still reports a make block that fails to provide the required methods — only the declaration of intent moves earlier.A second, separate fix was needed for BEAM. Lowering passes a trait dictionary derived from the call-site argument, so an omitted defaulted argument had none:
src/ir/lower.cxxnow takes the dictionary from the default expression when the argument is absent, at both dictionary sites. Without it this fix was walker-only.Spec:
spec/trait_default_param.kex, verified identical on both backends.