forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for type declarations on pointer types (gnolang#1733)
support type decl upon pointer type: ```go package main import "fmt" // Define a base type type Base int // Declare a new type that is a pointer to the base type type PtrToBase *Base func main() { var b Base = 42 // Initialize a variable of the base type var p PtrToBase = &b // Initialize a variable of the new pointer type with the address of b fmt.Printf("The value of b is: %d\n", b) // Using the new pointer type fmt.Printf("The value pointed to by p is: %d\n", *p) // Modifying the value pointed to by p *p = 100 fmt.Printf("The new value of b after modification through p is: %d\n", b) } // Output: // The value of b is: 42 // The value pointed to by p is: 42 // The new value of b after modification through p is: 100 ```
- Loading branch information
1 parent
327d30e
commit ca6566f
Showing
18 changed files
with
325 additions
and
6 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
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
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,28 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
// Define a base type | ||
type Base int | ||
|
||
// Declare a new type that is a pointer to the base type | ||
type PtrToBase *Base | ||
|
||
func main() { | ||
var b Base = 42 // Initialize a variable of the base type | ||
var p PtrToBase = &b // Initialize a variable of the new pointer type with the address of b | ||
|
||
fmt.Printf("The value of b is: %d\n", b) | ||
|
||
// Using the new pointer type | ||
fmt.Printf("The value pointed to by p is: %d\n", *p) | ||
|
||
// Modifying the value pointed to by p | ||
*p = 100 | ||
fmt.Printf("The new value of b after modification through p is: %d\n", b) | ||
} | ||
|
||
// Output: | ||
// The value of b is: 42 | ||
// The value pointed to by p is: 42 | ||
// The new value of b after modification through p is: 100 |
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,12 @@ | ||
package main | ||
|
||
type BytePtr *byte | ||
|
||
func main() { | ||
bs := []byte("hello") | ||
var p BytePtr = &bs[0] | ||
println(*p) | ||
} | ||
|
||
// Output: | ||
// 104 |
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,15 @@ | ||
package main | ||
|
||
type IntPtr *int | ||
|
||
func main() { | ||
var a, b int | ||
a = 1 // Set a to 104 | ||
s := []IntPtr{} | ||
s = append(s, &a) | ||
s = append(s, &b) | ||
println(*s[0]) | ||
} | ||
|
||
// Output: | ||
// 1 |
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,17 @@ | ||
package main | ||
|
||
type Arr [2]int | ||
type Ptr *Arr | ||
|
||
func main() { | ||
var arr Arr | ||
arr[0] = 0 | ||
arr[1] = 1 | ||
|
||
p := Ptr(&arr) | ||
|
||
println(p[:]) | ||
} | ||
|
||
// Output: | ||
// slice[(0 int),(1 int)] |
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,20 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type IntArray []int | ||
type Arr *IntArray | ||
|
||
func (a Arr) Add(x int) { // receiver is val, not ptr | ||
*a = append(*a, x) | ||
} | ||
|
||
func main() { | ||
a := new(IntArray) | ||
Arr(a).Add(4) | ||
|
||
fmt.Println(*a) | ||
} | ||
|
||
// Error: | ||
// main/files/type37.gno:8: invalid receiver type main.Arr (base type is pointer type) |
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,19 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type IntArray []int | ||
|
||
func (a *IntArray) Add(x int) { // receiver is val, not ptr | ||
*a = append(*a, x) | ||
} | ||
|
||
func main() { | ||
a := new(IntArray) | ||
a.Add(4) | ||
|
||
fmt.Println(*a) | ||
} | ||
|
||
// Output: | ||
// [4] |
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,22 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Integer int | ||
|
||
func (i **Integer) Add(x int) { | ||
**i += Integer(x) // Dereference twice to get to the actual Integer value and modify it | ||
} | ||
|
||
func main() { | ||
a := new(Integer) // a is a pointer to Integer | ||
b := &a // b is a pointer to a pointer to Integer | ||
|
||
// Since Add is defined on **Integer, you need to pass b | ||
b.Add(4) // Adds 4 to the value **b points to | ||
|
||
fmt.Println(**b) // Should print 4, as **b is the same as *a | ||
} | ||
|
||
// Error: | ||
// main/files/type37b.gno:7: invalid receiver type **main.Integer (base type is pointer type) |
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,20 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Integer int | ||
|
||
func (i *Integer) Add(x int) { // receiver is val, not ptr | ||
println(int(*i) + x) | ||
} | ||
|
||
func main() { | ||
a := new(Integer) | ||
a.Add(4) | ||
|
||
fmt.Println(*a) | ||
} | ||
|
||
// Output: | ||
// 4 | ||
// 0 |
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,16 @@ | ||
package main | ||
|
||
type IntPtr *int | ||
|
||
var ip IntPtr = new(int) | ||
|
||
func (p IntPtr) Int() int { | ||
return *p | ||
} | ||
|
||
func main() { | ||
println(ip.Int()) | ||
} | ||
|
||
// Error: | ||
// main/files/type37d.gno:7: invalid receiver type main.IntPtr (base type is pointer type) |
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,17 @@ | ||
package main | ||
|
||
type IntPtr *int | ||
type Int2 IntPtr | ||
|
||
var ip IntPtr = new(int) | ||
|
||
func (i2 Int2) Int() int { | ||
return *i2 | ||
} | ||
|
||
func main() { | ||
println(Int2(ip).Int()) | ||
} | ||
|
||
// Error: | ||
// main/files/type37e.gno:8: invalid receiver type main.Int2 (base type is pointer type) |
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,16 @@ | ||
package main | ||
|
||
type IntPtr *int | ||
|
||
var ip IntPtr = new(int) | ||
|
||
func (p *IntPtr) Int() int { | ||
return **p | ||
} | ||
|
||
func main() { | ||
println((&ip).Int()) | ||
} | ||
|
||
// Error: | ||
// main/files/type37f.gno:7: invalid receiver type *main.IntPtr (base type is pointer type) |
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,26 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type IntArray []int | ||
type Arr *IntArray | ||
|
||
func add(arr Arr) { // receiver is val, not ptr | ||
*arr = append(*arr, 1) | ||
} | ||
|
||
func main() { | ||
a := new(IntArray) | ||
add(a) | ||
|
||
fmt.Println(a) | ||
fmt.Println(*a) | ||
fmt.Println(len(*a)) | ||
fmt.Println((*a)[0]) | ||
} | ||
|
||
// Output: | ||
// &[1] | ||
// [1] | ||
// 1 | ||
// 1 |
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,22 @@ | ||
package main | ||
|
||
type foo interface { | ||
say() | ||
} | ||
|
||
func (f foo) echo() int { | ||
return 1 | ||
} | ||
|
||
type Bar struct{} | ||
|
||
func (b *Bar) say() {} | ||
|
||
func main() { | ||
var f foo | ||
f = &Bar{} | ||
println(f.echo()) | ||
} | ||
|
||
// Error: | ||
// main/files/type39.gno:7: invalid receiver type main.foo (base type is interface type) |
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,24 @@ | ||
package main | ||
|
||
type foo interface { | ||
say() | ||
} | ||
|
||
type FF foo | ||
|
||
func (f FF) echo() int { | ||
return 1 | ||
} | ||
|
||
type Bar struct{} | ||
|
||
func (b *Bar) say() {} | ||
|
||
func main() { | ||
var f foo | ||
f = &Bar{} | ||
println(f.echo()) | ||
} | ||
|
||
// Error: | ||
// main/files/type39a.gno:9: invalid receiver type main.FF (base type is interface type) |
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,22 @@ | ||
package main | ||
|
||
type foo interface { | ||
say() | ||
} | ||
|
||
func (f *foo) echo() int { | ||
return 1 | ||
} | ||
|
||
type Bar struct{} | ||
|
||
func (b *Bar) say() {} | ||
|
||
func main() { | ||
var f *foo | ||
*f = &Bar{} | ||
println(f.echo()) | ||
} | ||
|
||
// Error: | ||
// main/files/type39b.gno:7: invalid receiver type *main.foo (base type is interface type) |