ADR 0038: Members are reached through `self.`
Status: Accepted (2026-10-25). Amends 0031 (“fields and methods also remain in scope by name inside a method”). Breaking: a bare field or method name inside a method no longer means the member.
Context
Section titled “Context”Inside a method, fields and methods of the receiver are in scope by bare name.
Count += n means self.Count += n, and authed(list) means
self.authed(self.list). The checker looks receiver members up before
package-level names, imports, and types (lookup in internal/check/check.go).
The review found four kinds of problem with that rule.
-
Existing code silently changes meaning. A member wins over every name outside the method:
var limit = 50struct Query { limit: int }func Query.pageSize(): int {if self.limit > 0 { return self.limit }return limit // the field (0), not the package variable (50)}- With a field named
logand an import oflog,log.Println(…)calls the field. - A method
validatehides a package functionvalidate. - So adding a field or method to a type can change existing method bodies with no diagnostic.
- With a field named
-
Types and imports become unreachable. With a field
Config: Config, the type is unreachable inside the type’s methods:next: Config = …is “Config is not a type”. A methodStats(): Statshides its own result type. A field namedhttphidesnet/http. These are everyday Go layouts, and none of the errors mentionself. -
A safety check is skipped. In a value-receiver method,
self.hits.Add(1)is correctly E0502 (a pointer method on a copy). The barehits.Add(1)compiled and incremented a copy (hits: 0). The mutex version raced and deadlocked underlyth test -race. -
Warnings and bugs exist only because of the rule.
- W0006 fires when a local reuses a field name (
cfg := cfg.Load()). - A lambda-scope bug was fixed in the release polish: a lambda in a method read a field instead of an enclosing local of the same name.
- In concurrent code,
mu.Lock(); seq++; jobs.send(job)hides which names are shared state.
- W0006 fires when a local reuses a field name (
Go has no implicit receiver: every member access is s.x. Go reviewers rely on
that, and the pitch of Lyth is code that is easy to review.
Decision
Section titled “Decision”- Members need
self.Inside a method, fields and methods of the receiver are reached only throughself:self.Count,self.authed(self.list). A bare name resolves exactly as it would outside the method: locals and parameters, then package-level declarations and imports, then predeclared names. - No exception for methods. The rule covers calls to other methods
(
self.validate()) and method values (self.list) as well as fields. A single rule is easier to read and to teach than “fields needself, methods don’t”. - W0006 no longer has a field case. A local can no longer hide a field. It still warns when a local hides an outer local or parameter.
- The value-receiver checks always apply. Every member access now goes
through
self, so E0502 and E0504 apply to all of them. That closes the copy-mutation hole in point 3 with no separate fix. - Generated Go is unchanged. The receiver is still named after the type
(
s.Count).
Alternatives
Section titled “Alternatives”- Keep bare names (status quo). Shortest, and familiar from Kotlin, Swift, and Java. It keeps all four problems: the first two can be patched with errors, the last two cannot.
- Keep bare names, but make collisions an error. When a bare name
could also mean a local, import, package-level declaration, or type, report
an error. This removes the silent cases (points 1 and 2) without changing
most code, but readers still cannot tell
authed(list)from a function call orseq++from a package counter. The value-receiver check would still need fixing separately. This is the fallback if this ADR is rejected. - Go’s explicit receiver names (
func (s *Server) M()). ADR 0031 rejected them: a different name in every method, which locals can shadow.selfis the same idea with one name. - A shorter sigil (
.count,@count). Shorter thanself., but it is new syntax with no Go counterpart, and.xalready means something else in Swift (an implicit enum member).
Consequences
Section titled “Consequences”- Cost. Member accesses cost five characters; the line count does not
change.
examples/todoapihas about 60 implicit member accesses in 29 methods. Expression bodies on small value types read longer:func Point.Len(): float64 = math.Sqrt(self.X * self.X + self.Y * self.Y). Go writesp.X*p.X. - Readability. Readers see shared state and method calls at a glance, as in Go.
- Names no longer collide. A field may share its name with a type, an import, or a package-level declaration.
- Interaction with embedding. Promoted members come through
selfas well (self.Header()), so embedding does not widen the set of bare names in scope (ADR 0043).
Migration
Section titled “Migration”The old resolution is still computed, so the change can be made without any silent difference:
- Bare member names are errors for at least the following releases. A bare
name that the old rule resolved to a member is an error (“count is a field
of self; write self.count”), whose edit inserts
self.. This holds even when the name now also resolves to something else, so the package-levellimitnever takes over silently. - The edit is a fix only where it is unambiguous. Where the name can mean
only the member, the edit respells what the code meant, and
lyth fixmigrates a module in one pass. Where the name also names something valid in that position (a package-level variable, constant, or function; an import left of a dot; a type or builtin that is called), code written before this ADR meant the member but code written since may mean the other name. The error names both (“limit is a field of self and also names the package-level variable limit”), the edit is a hint, andlyth fixleaves the choice to the author. - A called builtin is the builtin, with a warning. Amended after porting
Go packages: a type with a method
append(or a function fieldprint) left the builtin unreachable in all of its methods, since nothing qualifies a builtin. A bare call of a builtin there now calls the builtin, with warning W0011 namingself.appendfor the member, while code from before this ADR is migrated. A package-level name can be renamed or reached otherwise, so the other collisions stay errors. - Calls of fields that are not functions cannot have meant the member,
so they take the new meaning without an error during the migration window,
because calling such a field never compiled:
Config(Name = "x")with a fieldConfig: Configconstructs the type,max(a, b)with a fieldmax: intcalls the builtin, andstrings.Join(...)with a fieldstrings: []string(which has noJoin) calls the package. - After that, bare names resolve normally (locals, then package-level), as in any explicit-receiver language.
- Docs. A
testdata/fixcase and a row in the guide’s version table. The guide, tour, cheat sheet, spec (§3.2), examples, std, bench, and evals reference solutions are migrated bylyth fixin the same change.
Implementation
Section titled “Implementation”- Lookup.
lookupininternal/check/check.gostops consultingreceiverMemberand uses it only to produce the migration error. - W0006. Its field case goes from
internal/check/stmt.go. - Tests.
check/members_neg: bare fields and methods, collisions with a package variable, a function, an import, a type, and a builtin, lambdas, method values, and locals of a member’s name.fix/self:lyth fixinsertsself.where only the member is meant, and leaves calls of a type or builtin named like a field.TestFixLeavesAmbiguousMembers:lyth fixleaves ambiguous names, and a second run changes nothing.run/selfnames: fields named like a type, an import, and a local.- Every Lyth source in the repository was migrated with the fix.