Set internal linkage and keeping default visibility for anonymous functions

This commit is contained in:
Phil Kedy
2022-07-28 10:34:27 -04:00
committed by Ron Evans
parent 25c8d3ec3a
commit 05cdde162c
10 changed files with 123 additions and 9 deletions
+8
View File
@@ -1,11 +1,19 @@
package main
import (
"github.com/tinygo-org/tinygo/testdata/generics/testa"
"github.com/tinygo-org/tinygo/testdata/generics/testb"
)
func main() {
println("add:", Add(3, 5))
println("add:", Add(int8(3), 5))
var c C[int]
c.F() // issue 2951
testa.Test()
testb.Test()
}
type Integer interface {
+4
View File
@@ -1,2 +1,6 @@
add: 8
add: 8
value: 101
value: 101
value: 501
value: 501
+20
View File
@@ -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
}
+20
View File
@@ -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
}
+53
View File
@@ -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))
})
}