-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: improve handling of generic types
When generating a new type, the parameter type was not correctly duplicated in the new AST. This is fixed by making copyNode recursive if needed. The out of order processing of generic types has also been fixed. Fixes #1488
- Loading branch information
Showing
12 changed files
with
423 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/netip" | ||
) | ||
|
||
type Slice[T any] struct { | ||
x []T | ||
} | ||
|
||
type IPPrefixSlice struct { | ||
x Slice[netip.Prefix] | ||
} | ||
|
||
func (v Slice[T]) MarshalJSON() ([]byte, error) { return json.Marshal(v.x) } | ||
|
||
// MarshalJSON implements json.Marshaler. | ||
func (v IPPrefixSlice) MarshalJSON() ([]byte, error) { | ||
return v.x.MarshalJSON() | ||
} | ||
|
||
func main() { | ||
t := IPPrefixSlice{} | ||
fmt.Println(t) | ||
b, e := t.MarshalJSON() | ||
fmt.Println(string(b), e) | ||
} | ||
|
||
// Output: | ||
// {{[]}} | ||
// null <nil> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func MapOf[K comparable, V any](m map[K]V) Map[K, V] { | ||
return Map[K, V]{m} | ||
} | ||
|
||
type Map[K comparable, V any] struct { | ||
ж map[K]V | ||
} | ||
|
||
func (v MapView) Int() Map[string, int] { return MapOf(v.ж.Int) } | ||
|
||
type VMap struct { | ||
Int map[string]int | ||
} | ||
|
||
type MapView struct { | ||
ж *VMap | ||
} | ||
|
||
func main() { | ||
mv := MapView{&VMap{}} | ||
fmt.Println(mv.ж) | ||
} | ||
|
||
// Output: | ||
// &{map[]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
type Map[K comparable, V any] struct { | ||
ж map[K]V | ||
} | ||
|
||
func (m Map[K, V]) Has(k K) bool { | ||
_, ok := m.ж[k] | ||
return ok | ||
} | ||
|
||
func main() { | ||
m := Map[string, float64]{} | ||
println(m.Has("test")) | ||
} | ||
|
||
// Output: | ||
// false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type vector interface { | ||
[]int | [3]int | ||
} | ||
|
||
func sum[V vector](v V) (out int) { | ||
for i := 0; i < len(v); i++ { | ||
out += v[i] | ||
} | ||
return | ||
} | ||
|
||
func main() { | ||
va := [3]int{1, 2, 3} | ||
vs := []int{1, 2, 3} | ||
fmt.Println(sum[[3]int](va), sum[[]int](vs)) | ||
} | ||
|
||
// Output: | ||
// 6 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/traefik/yaegi/_test/p6" | ||
) | ||
|
||
func main() { | ||
t := p6.IPPrefixSlice{} | ||
fmt.Println(t) | ||
b, e := t.MarshalJSON() | ||
fmt.Println(string(b), e) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package p6 | ||
|
||
import ( | ||
"encoding/json" | ||
"net/netip" | ||
) | ||
|
||
type Slice[T any] struct { | ||
x []T | ||
} | ||
|
||
type IPPrefixSlice struct { | ||
x Slice[netip.Prefix] | ||
} | ||
|
||
func (v Slice[T]) MarshalJSON() ([]byte, error) { return json.Marshal(v.x) } | ||
|
||
// MarshalJSON implements json.Marshaler. | ||
func (v IPPrefixSlice) MarshalJSON() ([]byte, error) { | ||
return v.x.MarshalJSON() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.