compiler: support non-struct methods
There is no need to restrict ourselves just to structs.
This commit is contained in:
parent
87f6ba34db
commit
0629479560
2 changed files with 17 additions and 9 deletions
|
@ -304,15 +304,8 @@ func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl) {
|
|||
// to support other types.
|
||||
if decl.Recv != nil {
|
||||
for _, arg := range decl.Recv.List {
|
||||
ident := arg.Names[0]
|
||||
// Currently only method receives for struct types is supported.
|
||||
_, ok := c.typeInfo.Defs[ident].Type().Underlying().(*types.Struct)
|
||||
if !ok {
|
||||
c.prog.Err = fmt.Errorf("method receives for non-struct types is not yet supported")
|
||||
return
|
||||
}
|
||||
// only create an argument here, it will be stored via INITSLOT
|
||||
c.scope.newVariable(varArgument, ident.Name)
|
||||
c.scope.newVariable(varArgument, arg.Names[0].Name)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package compiler_test
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"math/big"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCustomType(t *testing.T) {
|
||||
src := `
|
||||
|
@ -22,3 +25,15 @@ func TestCustomType(t *testing.T) {
|
|||
`
|
||||
eval(t, src, []byte("some short string"))
|
||||
}
|
||||
|
||||
func TestCustomTypeMethods(t *testing.T) {
|
||||
src := `package foo
|
||||
type bar int
|
||||
func (b bar) add(a bar) bar { return a + b }
|
||||
func Main() bar {
|
||||
var b bar
|
||||
b = 10
|
||||
return b.add(32)
|
||||
}`
|
||||
eval(t, src, big.NewInt(42))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue