Skip to content

Test for functional argument passing #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions testdata/calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ type Thing struct {
name string
}

type ThingOption func(*Thing)

func WithName(name string) ThingOption {
return func(t *Thing) {
t.name = name
}
}

func NewThing(opts ...ThingOption) *Thing {
t := &Thing{}
for _, opt := range opts {
opt(t)
}
return t
}

func (t Thing) String() string {
return t.name
}
Expand Down Expand Up @@ -36,6 +52,12 @@ func main() {
runFunc(func(i int) {
println("inside fp closure:", thing.String(), i)
}, 3)

// functional arguments
thingFunctionalArgs1 := NewThing()
thingFunctionalArgs1.Print("functional args 1")
thingFunctionalArgs2 := NewThing(WithName("named thing"))
thingFunctionalArgs2.Print("functional args 2")
}

func runFunc(f func(int), arg int) {
Expand Down
2 changes: 2 additions & 0 deletions testdata/calls.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ Thing.Print: foo arg: bar
bound method: foo
thing inside closure: foo
inside fp closure: foo 3
Thing.Print: arg: functional args 1
Thing.Print: named thing arg: functional args 2