Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Method
- A function attached to a type and called on a value of it.
- Receiver
- The value a method is called on: this, self, or Go’s receiver parameter.
- Value receiver
- A receiver initialized from a value copy. Direct scalar-field writes affect that copy; copied pointer or reference fields can still lead to shared data.
- Bound method
- A method value that remembers which receiver to use.
- Bound receiver
- The object passed as a stored callback’s receiver. A stored receiver copy is a distinct object; its fields may still refer to other shared data.
The value behind a method call
A method is an operation associated with a type or object. Its receiver is the value the operation runs on: in log.appendSession(minutes), log supplies that receiver and minutes is an ordinary argument.
Reading a method, saving it for later and invoking it are separate steps. Trace what the saved callable retains, then which value the eventual call reaches; receiver rules differ between languages.
Go declares the receiver before the method name. A value receiver gets a copy, while a pointer receiver can reach the original. Copying a value can still share data through its pointer or slice fields; inspect the fields the method actually uses.
01The receiver
A method receives its object as a hidden or explicit first argument
A team imports work-session durations into personal logs. Each selected person attended the same sessions, so the importer must update every person’s existing log. A buffered replay goes through a recorder. Which value does each method receive?
The importer first needs one person’s existing log to retain new durations. Trace the receiver of the calls below and predict what the caller reads afterward.
package main
import "fmt"
type WorkLog struct {
Total int
}
func (t WorkLog) AppendSessionCopy(minutes int) {
t.Total += minutes
}
func (t *WorkLog) AppendSession(minutes int) {
t.Total += minutes
}
func main() {
var log WorkLog
log.AppendSessionCopy(20)
fmt.Println(log.Total)
log.AppendSession(20)
log.AppendSession(15)
fmt.Println(log.Total)
}
go run main.go
Use the setup from the first reading for your language.
02A method as a value
A method passed as a value keeps its receiver, a copy of it, or nothing
A buffered import stores an action for later replay. Before wiring it to a control, check what a saved method keeps: the live log, an earlier receiver value, or no receiver at all.
package main
import "fmt"
type WorkLog struct {
Total int
}
func (t WorkLog) Report() int {
return t.Total
}
func main() {
log := WorkLog{Total: 10}
report := log.Report
log.Total = 35
fmt.Println(report(), log.Report())
}
03No receiver
Functions that build or work on a type without a receiver
A new log can also be initialized from a batch before any instance is selected. These creation functions build the receiver state for later calls; predict the returned total.
package main
import "fmt"
type WorkLog struct {
Total int
}
func FromSessions(entries []int) WorkLog {
var log WorkLog
for _, entry := range entries {
log.Total += entry
}
return log
}
func main() {
fmt.Println(FromSessions([]int{20, 15}).Total)
}
04Repair the work-session import
One log, a team import, then buffered replay
First append durations to one person’s log. Then import each attended session into every selected person’s log and reconcile minutes and entry counts. Finally replay the buffered durations through another object into the same log type.
Each file contains its imports, reader and complete caller.
Repair the work-session import
Retain one log’s updates, reconcile all selected people’s logs, then replay through the intended live receiver.
In the frontend Register a callback that invokes the intended recorder method. Optional
A browser replay control receives a recorder from its client-side owner. Its ordinary flush method reads that recorder’s instance fields. Registering a callback is separate from choosing the object on which flush is called.
"use client";
type Recorder = { label: string; flush(): void };
export default function ReplayControl({ recorder }: { recorder: Recorder }) {
return <button onClick={() => recorder.flush()}>Replay {recorder.label}</button>;
}
The owner is another client-side component, so this function-valued recorder prop does not cross a server serialization boundary. On a click the wrapper performs recorder.flush(), supplying recorder as the method receiver. The recorder owns the replay work and any state publication.
Sources: React: responding to events,Svelte: event attributes,TypeScript: this in functions.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?