Skip to content

Commit fbb1c3f

Browse files
thehowlltzmaxwell
authored andcommitted
feat(gnovm): add any type (#3862)
This exists in Go, and it's a pain to add it later as a global, so making a PR to add this. extracted from #3847. --------- Co-authored-by: ltzmaxwell <[email protected]>
1 parent 552dce0 commit fbb1c3f

File tree

267 files changed

+1265
-1240
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

267 files changed

+1265
-1240
lines changed

docs/assets/how-to-guides/porting-solidity-to-gno/porting-1.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func shouldEqual(t *testing.T, got interface{}, expected interface{}) {
1+
func shouldEqual(t *testing.T, got any, expected any) {
22
t.Helper()
33

44
if got != expected {

docs/assets/how-to-guides/write-simple-dapp/poll-1.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (p *Poll) HasVoted(address std.Address) (bool, bool) {
5959
func (p Poll) VoteCount() (int, int) {
6060
var yay int
6161

62-
p.Voters().Iterate("", "", func(key string, value interface{}) bool {
62+
p.Voters().Iterate("", "", func(key string, value any) bool {
6363
vote := value.(bool)
6464
if vote == true {
6565
yay = yay + 1

docs/assets/how-to-guides/write-simple-dapp/poll-3.gno

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func Render(path string) string {
77
b.WriteString("### No active polls currently!")
88
return b.String()
99
}
10-
polls.Iterate("", "", func(key string, value interface{}) bool {
10+
polls.Iterate("", "", func(key string, value any) bool {
1111

1212
// cast raw data from tree into Poll struct
1313
p := value.(*poll.Poll)
@@ -50,7 +50,7 @@ func Render(path string) string {
5050
dropdown = "<br><details>\n<summary>Vote details</summary>"
5151
b.WriteString(dropdown)
5252

53-
p.Voters().Iterate("", "", func(key string, value interface{}) bool {
53+
p.Voters().Iterate("", "", func(key string, value any) bool {
5454

5555
voter := key
5656
vote := value.(bool)

docs/how-to-guides/porting-solidity-to-gno.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Below is a test case helper that will help implement each condition.
180180

181181
[embedmd]:# (../assets/how-to-guides/porting-solidity-to-gno/porting-1.gno go)
182182
```go
183-
func shouldEqual(t *testing.T, got interface{}, expected interface{}) {
183+
func shouldEqual(t *testing.T, got any, expected any) {
184184
t.Helper()
185185

186186
if got != expected {

docs/how-to-guides/write-simple-dapp.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (p *Poll) HasVoted(address std.Address) (bool, bool) {
8989
func (p Poll) VoteCount() (int, int) {
9090
var yay int
9191

92-
p.Voters().Iterate("", "", func(key string, value interface{}) bool {
92+
p.Voters().Iterate("", "", func(key string, value any) bool {
9393
vote := value.(bool)
9494
if vote == true {
9595
yay = yay + 1
@@ -229,7 +229,7 @@ func Render(path string) string {
229229
b.WriteString("### No active polls currently!")
230230
return b.String()
231231
}
232-
polls.Iterate("", "", func(key string, value interface{}) bool {
232+
polls.Iterate("", "", func(key string, value any) bool {
233233

234234
// cast raw data from tree into Poll struct
235235
p := value.(*poll.Poll)
@@ -272,7 +272,7 @@ func Render(path string) string {
272272
dropdown = "<br><details>\n<summary>Vote details</summary>"
273273
b.WriteString(dropdown)
274274

275-
p.Voters().Iterate("", "", func(key string, value interface{}) bool {
275+
p.Voters().Iterate("", "", func(key string, value any) bool {
276276

277277
voter := key
278278
vote := value.(bool)

docs/reference/go-gno-compatibility.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ rune := rune('a')
5454
| `uintptr`, `unsafe.Pointer` | missing | missing |
5555
| `string` | full | full |
5656
| `rune` | full | full |
57-
| `interface{}` | full | full |
57+
| `interface{}` / `any` | full | full |
5858
| `[]T` (slices) | full | full\* |
5959
| `[N]T` (arrays) | full | full\* |
6060
| `map[T1]T2` | full | full\* |

examples/gno.land/p/demo/avl/list/list.gno

+18-18
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// l.Delete(0) // removes first element
2020
//
2121
// // Iterate over elements
22-
// l.ForEach(func(index int, value interface{}) bool {
22+
// l.ForEach(func(index int, value any) bool {
2323
// ufmt.Printf("index %d: %v\n", index, value)
2424
// return false // continue iteration
2525
// })
@@ -44,12 +44,12 @@ import (
4444
// IList defines the interface for list operations
4545
type IList interface {
4646
Len() int
47-
Append(values ...interface{})
48-
Get(index int) interface{}
49-
Set(index int, value interface{}) bool
50-
Delete(index int) (interface{}, bool)
51-
Slice(startIndex, endIndex int) []interface{}
52-
ForEach(fn func(index int, value interface{}) bool)
47+
Append(values ...any)
48+
Get(index int) any
49+
Set(index int, value any) bool
50+
Delete(index int) (any, bool)
51+
Slice(startIndex, endIndex int) []any
52+
ForEach(fn func(index int, value any) bool)
5353
Clone() *List
5454
DeleteRange(startIndex, endIndex int) int
5555
}
@@ -82,7 +82,7 @@ func (l *List) Len() int {
8282
// l.Append(1) // adds single value
8383
// l.Append(2, 3, 4) // adds multiple values
8484
// println(l.Len()) // Output: 4
85-
func (l *List) Append(values ...interface{}) {
85+
func (l *List) Append(values ...any) {
8686
for _, v := range values {
8787
l.tree.Set(l.idGen.Next().String(), v)
8888
}
@@ -98,7 +98,7 @@ func (l *List) Append(values ...interface{}) {
9898
// println(l.Get(1)) // Output: 2
9999
// println(l.Get(-1)) // Output: nil
100100
// println(l.Get(999)) // Output: nil
101-
func (l *List) Get(index int) interface{} {
101+
func (l *List) Get(index int) any {
102102
if index < 0 || index >= l.tree.Size() {
103103
return nil
104104
}
@@ -123,7 +123,7 @@ func (l *List) Get(index int) interface{} {
123123
//
124124
// l.Set(-1, 5) // invalid index
125125
// println(l.Len()) // Output: 4 (list unchanged)
126-
func (l *List) Set(index int, value interface{}) bool {
126+
func (l *List) Set(index int, value any) bool {
127127
size := l.tree.Size()
128128

129129
// Handle empty list case - only allow index 0
@@ -170,7 +170,7 @@ func (l *List) Set(index int, value interface{}) bool {
170170
//
171171
// val, ok = l.Delete(-1)
172172
// println(val, ok) // Output: nil false
173-
func (l *List) Delete(index int) (interface{}, bool) {
173+
func (l *List) Delete(index int) (any, bool) {
174174
size := l.tree.Size()
175175
// Always return nil, false for empty list
176176
if size == 0 {
@@ -202,7 +202,7 @@ func (l *List) Delete(index int) (interface{}, bool) {
202202
// println(l.Slice(-1, 2)) // Output: [1 2]
203203
// println(l.Slice(3, 999)) // Output: [4 5]
204204
// println(l.Slice(3, 2)) // Output: nil
205-
func (l *List) Slice(startIndex, endIndex int) []interface{} {
205+
func (l *List) Slice(startIndex, endIndex int) []any {
206206
size := l.tree.Size()
207207

208208
// Normalize bounds
@@ -217,10 +217,10 @@ func (l *List) Slice(startIndex, endIndex int) []interface{} {
217217
}
218218

219219
count := endIndex - startIndex
220-
result := make([]interface{}, count)
220+
result := make([]any, count)
221221

222222
i := 0
223-
l.tree.IterateByOffset(startIndex, count, func(_ string, value interface{}) bool {
223+
l.tree.IterateByOffset(startIndex, count, func(_ string, value any) bool {
224224
result[i] = value
225225
i++
226226
return false
@@ -229,13 +229,13 @@ func (l *List) Slice(startIndex, endIndex int) []interface{} {
229229
}
230230

231231
// ForEach iterates through all elements in the list.
232-
func (l *List) ForEach(fn func(index int, value interface{}) bool) {
232+
func (l *List) ForEach(fn func(index int, value any) bool) {
233233
if l.tree.Size() == 0 {
234234
return
235235
}
236236

237237
index := 0
238-
l.tree.IterateByOffset(0, l.tree.Size(), func(_ string, value interface{}) bool {
238+
l.tree.IterateByOffset(0, l.tree.Size(), func(_ string, value any) bool {
239239
result := fn(index, value)
240240
index++
241241
return result
@@ -265,7 +265,7 @@ func (l *List) Clone() *List {
265265
return newList
266266
}
267267

268-
l.tree.IterateByOffset(0, size, func(_ string, value interface{}) bool {
268+
l.tree.IterateByOffset(0, size, func(_ string, value any) bool {
269269
newList.Append(value)
270270
return false
271271
})
@@ -300,7 +300,7 @@ func (l *List) DeleteRange(startIndex, endIndex int) int {
300300

301301
// Collect keys to delete
302302
keysToDelete := make([]string, 0, endIndex-startIndex)
303-
l.tree.IterateByOffset(startIndex, endIndex-startIndex, func(key string, _ interface{}) bool {
303+
l.tree.IterateByOffset(startIndex, endIndex-startIndex, func(key string, _ any) bool {
304304
keysToDelete = append(keysToDelete, key)
305305
return false
306306
})

examples/gno.land/p/demo/avl/list/list_test.gno

+18-18
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestList_Set(t *testing.T) {
6969
}
7070

7171
// Verify list state hasn't changed after invalid operations
72-
expected := []interface{}{1, 42, 3, 4}
72+
expected := []any{1, 42, 3, 4}
7373
for i, want := range expected {
7474
if got := l.Get(i); got != want {
7575
t.Errorf("index %d = %v; want %v", i, got, want)
@@ -107,16 +107,16 @@ func TestList_Slice(t *testing.T) {
107107

108108
// Test valid ranges
109109
values := l.Slice(1, 4)
110-
expected := []interface{}{2, 3, 4}
110+
expected := []any{2, 3, 4}
111111
if !sliceEqual(values, expected) {
112112
t.Errorf("Slice(1,4) = %v; want %v", values, expected)
113113
}
114114

115115
// Test edge cases
116-
if values := l.Slice(-1, 2); !sliceEqual(values, []interface{}{1, 2}) {
116+
if values := l.Slice(-1, 2); !sliceEqual(values, []any{1, 2}) {
117117
t.Errorf("Slice(-1,2) = %v; want [1 2]", values)
118118
}
119-
if values := l.Slice(3, 10); !sliceEqual(values, []interface{}{4, 5}) {
119+
if values := l.Slice(3, 10); !sliceEqual(values, []any{4, 5}) {
120120
t.Errorf("Slice(3,10) = %v; want [4 5]", values)
121121
}
122122
if values := l.Slice(3, 2); values != nil {
@@ -129,7 +129,7 @@ func TestList_ForEach(t *testing.T) {
129129
l.Append(1, 2, 3)
130130

131131
sum := 0
132-
l.ForEach(func(index int, value interface{}) bool {
132+
l.ForEach(func(index int, value any) bool {
133133
sum += value.(int)
134134
return false
135135
})
@@ -140,7 +140,7 @@ func TestList_ForEach(t *testing.T) {
140140

141141
// Test early termination
142142
count := 0
143-
l.ForEach(func(index int, value interface{}) bool {
143+
l.ForEach(func(index int, value any) bool {
144144
count++
145145
return true // stop after first item
146146
})
@@ -187,7 +187,7 @@ func TestList_DeleteRange(t *testing.T) {
187187
if l.Len() != 2 {
188188
t.Errorf("after DeleteRange(1,4) len = %d; want 2", l.Len())
189189
}
190-
expected := []interface{}{1, 5}
190+
expected := []any{1, 5}
191191
for i, want := range expected {
192192
if got := l.Get(i); got != want {
193193
t.Errorf("after DeleteRange(1,4) index %d = %v; want %v", i, got, want)
@@ -281,7 +281,7 @@ func TestList_LargeOperations(t *testing.T) {
281281

282282
// Test range on large list
283283
values := l.Slice(n-3, n)
284-
expected := []interface{}{n - 3, n - 2, n - 1}
284+
expected := []any{n - 3, n - 2, n - 1}
285285
if !sliceEqual(values, expected) {
286286
t.Errorf("Range(%d,%d) = %v; want %v", n-3, n, values, expected)
287287
}
@@ -305,7 +305,7 @@ func TestList_ChainedOperations(t *testing.T) {
305305
l.Append(4)
306306
l.Set(1, 5)
307307

308-
expected := []interface{}{1, 5, 4}
308+
expected := []any{1, 5, 4}
309309
for i, want := range expected {
310310
if got := l.Get(i); got != want {
311311
t.Errorf("index %d = %v; want %v", i, got, want)
@@ -320,10 +320,10 @@ func TestList_RangeEdgeCases(t *testing.T) {
320320
// Test various edge cases for Range
321321
cases := []struct {
322322
start, end int
323-
want []interface{}
323+
want []any
324324
}{
325-
{-10, 2, []interface{}{1, 2}},
326-
{3, 10, []interface{}{4, 5}},
325+
{-10, 2, []any{1, 2}},
326+
{3, 10, []any{4, 5}},
327327
{0, 0, nil},
328328
{5, 5, nil},
329329
{4, 3, nil},
@@ -357,7 +357,7 @@ func TestList_IndexConsistency(t *testing.T) {
357357
l.Append(8, 9, 10) // [1,6,7,8,9,10]
358358

359359
// Verify sequence is continuous
360-
expected := []interface{}{1, 6, 7, 8, 9, 10}
360+
expected := []any{1, 6, 7, 8, 9, 10}
361361
for i, want := range expected {
362362
if got := l.Get(i); got != want {
363363
t.Errorf("index %d = %v; want %v", i, got, want)
@@ -376,9 +376,9 @@ func TestList_IndexConsistency(t *testing.T) {
376376
}
377377

378378
// Verify no gaps in iteration
379-
var iteratedValues []interface{}
379+
var iteratedValues []any
380380
var indices []int
381-
l.ForEach(func(index int, value interface{}) bool {
381+
l.ForEach(func(index int, value any) bool {
382382
iteratedValues = append(iteratedValues, value)
383383
indices = append(indices, index)
384384
return false
@@ -408,7 +408,7 @@ func TestList_RecursiveSafety(t *testing.T) {
408408

409409
// Test deep list traversal
410410
found := false
411-
l.ForEach(func(i int, v interface{}) bool {
411+
l.ForEach(func(i int, v any) bool {
412412
if str, ok := v.(string); ok {
413413
if str == "id2" {
414414
found = true
@@ -432,7 +432,7 @@ func TestList_RecursiveSafety(t *testing.T) {
432432
if !short {
433433
// Search for a value
434434
var lastFound bool
435-
l.ForEach(func(j int, v interface{}) bool {
435+
l.ForEach(func(j int, v any) bool {
436436
if str, ok := v.(string); ok {
437437
if str == ufmt.Sprintf("id%d", i+3) {
438438
lastFound = true
@@ -460,7 +460,7 @@ func TestList_RecursiveSafety(t *testing.T) {
460460
}
461461

462462
// Helper function to compare slices
463-
func sliceEqual(a, b []interface{}) bool {
463+
func sliceEqual(a, b []any) bool {
464464
if len(a) != len(b) {
465465
return false
466466
}

0 commit comments

Comments
 (0)