Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions type_tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ func (p *CodeBuilder) LookupField(t *types.Struct, name string) int {
return -1
}

// TupleLit creates a tuple literal.
func (p *CodeBuilder) TupleLit(typ types.Type, arity int, src ...ast.Node) *CodeBuilder {
if typ == nil {
pkg := p.pkg
pkgTypes := pkg.Types
args := p.stk.GetArgs(arity)
flds := make([]*types.Var, arity)
for i := 0; i < arity; i++ {
fldt := types.Default(args[i].Type)
flds[i] = types.NewField(token.NoPos, pkgTypes, "", fldt, false)
}
typ = pkg.NewTuple(false, flds...)
Comment on lines +179 to +187
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The if typ == nil block can be simplified by removing the intermediate variables pkg and pkgTypes. You can directly use p.pkg where these variables are used to make the code more concise.

args := p.stk.GetArgs(arity)
		flds := make([]*types.Var, arity)
		for i := 0; i < arity; i++ {
			fldt := types.Default(args[i].Type)
			flds[i] = types.NewField(token.NoPos, p.pkg.Types, "", fldt, false)
		}
		typ = p.pkg.NewTuple(false, flds...)

}
return p.StructLit(typ, arity, false, src...)
}

// NewTuple creates a tuple type with the given fields.
// The fields are named as X_0, X_1, ...
// If withName is true, the original fields can also be accessed by their
Expand Down
19 changes: 19 additions & 0 deletions type_tuple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ import (
"github.com/goplus/gogen"
)

func TestTupleLit(t *testing.T) {
pkg := newMainPackage()
pkg.NewFunc(nil, "foo", nil, nil, false).BodyStart(pkg).
Val(1).
Val(2).
TupleLit(nil, 2).
EndStmt().
End()
domTest(t, pkg, `package main

func foo() {
struct {
X_0 int
X_1 int
}{1, 2}
}
`)
}

func TestTupleMember(t *testing.T) {
pkg := newMainPackage()
x := types.NewField(token.NoPos, pkg.Types, "x", types.Typ[types.Int], false)
Expand Down
Loading