mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 23:13:40 +00:00
Test for functional argument passing (#336)
* Test for functional argument passing
This commit is contained in:
Vendored
+22
@@ -4,6 +4,22 @@ type Thing struct {
|
|||||||
name string
|
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 {
|
func (t Thing) String() string {
|
||||||
return t.name
|
return t.name
|
||||||
}
|
}
|
||||||
@@ -36,6 +52,12 @@ func main() {
|
|||||||
runFunc(func(i int) {
|
runFunc(func(i int) {
|
||||||
println("inside fp closure:", thing.String(), i)
|
println("inside fp closure:", thing.String(), i)
|
||||||
}, 3)
|
}, 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) {
|
func runFunc(f func(int), arg int) {
|
||||||
|
|||||||
Vendored
+2
@@ -7,3 +7,5 @@ Thing.Print: foo arg: bar
|
|||||||
bound method: foo
|
bound method: foo
|
||||||
thing inside closure: foo
|
thing inside closure: foo
|
||||||
inside fp closure: foo 3
|
inside fp closure: foo 3
|
||||||
|
Thing.Print: arg: functional args 1
|
||||||
|
Thing.Print: named thing arg: functional args 2
|
||||||
|
|||||||
Reference in New Issue
Block a user