aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata/generics
diff options
context:
space:
mode:
authorPhil Kedy <[email protected]>2022-07-28 10:34:27 -0400
committerRon Evans <[email protected]>2022-08-01 10:53:48 +0200
commit05cdde162c2a983fa5cc504cbb6bf9dedfda81ab (patch)
tree40d2f82574927b09bb68f97554fc095015a8fefe /testdata/generics
parent25c8d3ec3a80fb069fcd96169ab0fb60902cff22 (diff)
downloadtinygo-05cdde162c2a983fa5cc504cbb6bf9dedfda81ab.tar.gz
tinygo-05cdde162c2a983fa5cc504cbb6bf9dedfda81ab.zip
Set internal linkage and keeping default visibility for anonymous functions
Diffstat (limited to 'testdata/generics')
-rw-r--r--testdata/generics/testa/testa.go20
-rw-r--r--testdata/generics/testb/testb.go20
-rw-r--r--testdata/generics/value/value.go53
3 files changed, 93 insertions, 0 deletions
diff --git a/testdata/generics/testa/testa.go b/testdata/generics/testa/testa.go
new file mode 100644
index 000000000..87cca737a
--- /dev/null
+++ b/testdata/generics/testa/testa.go
@@ -0,0 +1,20 @@
+package testa
+
+import (
+ "github.com/tinygo-org/tinygo/testdata/generics/value"
+)
+
+func Test() {
+ v := value.New(1)
+ vm := value.Map(v, Plus100)
+ vm.Get(callback, callback)
+}
+
+func callback(v int) {
+ println("value:", v)
+}
+
+// Plus100 is a `Transform` that adds 100 to `value`.
+func Plus100(value int) int {
+ return value + 100
+}
diff --git a/testdata/generics/testb/testb.go b/testdata/generics/testb/testb.go
new file mode 100644
index 000000000..263c7ce39
--- /dev/null
+++ b/testdata/generics/testb/testb.go
@@ -0,0 +1,20 @@
+package testb
+
+import (
+ "github.com/tinygo-org/tinygo/testdata/generics/value"
+)
+
+func Test() {
+ v := value.New(1)
+ vm := value.Map(v, Plus500)
+ vm.Get(callback, callback)
+}
+
+func callback(v int) {
+ println("value:", v)
+}
+
+// Plus500 is a `Transform` that adds 500 to `value`.
+func Plus500(value int) int {
+ return value + 500
+}
diff --git a/testdata/generics/value/value.go b/testdata/generics/value/value.go
new file mode 100644
index 000000000..5348f1848
--- /dev/null
+++ b/testdata/generics/value/value.go
@@ -0,0 +1,53 @@
+package value
+
+type (
+ Value[T any] interface {
+ Get(Callback[T], Callback[T])
+ }
+
+ Callback[T any] func(T)
+
+ Transform[S any, D any] func(S) D
+)
+
+func New[T any](v T) Value[T] {
+ return &value[T]{
+ v: v,
+ }
+}
+
+type value[T any] struct {
+ v T
+}
+
+func (v *value[T]) Get(fn1, fn2 Callback[T]) {
+ // For example purposes.
+ // Normally would be asynchronous callback.
+ fn1(v.v)
+ fn2(v.v)
+}
+
+func Map[S, D any](v Value[S], tx Transform[S, D]) Value[D] {
+ return &mapper[S, D]{
+ v: v,
+ tx: tx,
+ }
+}
+
+type mapper[S, D any] struct {
+ v Value[S]
+ tx Transform[S, D]
+}
+
+func (m *mapper[S, D]) Get(fn1, fn2 Callback[D]) {
+ // two callbacks are passed to generate more than
+ // one anonymous function symbol name.
+ m.v.Get(func(v S) {
+ // anonymous function inside of anonymous function.
+ func() {
+ fn1(m.tx(v))
+ }()
+ }, func(v S) {
+ fn2(m.tx(v))
+ })
+}