Variadic functions with variable number of struct inputs now working.

This commit is contained in:
Nishant Sharma 2022-06-06 19:42:36 +05:30
parent ece09eda60
commit 04688ca746
4 changed files with 47 additions and 13 deletions

View File

@ -3,11 +3,17 @@
# license that can be found in the LICENSE file.
import variadic, go
############### Non Variadic ##############
nonvarResult = variadic.NonVariFunc(1, go.Slice_int([2,3,4]),5)
print("NonVariadic 1+[2+3+4]+5 = %d" % nonvarResult)
############### Variadic Over Int ##############
varResult = variadic.VariFunc(1,2,3,4,5)
print("Variadic 1+2+3+4+5 = %d" % varResult)
nonvarResult = variadic.NonVariFunc(1, go.Slice_int([2,3,4]),5)
print("NonVariadic 1+[2+3+4]+5 = %d" % nonvarResult)
############### Variadic Over Struct ##############
varStructResult = variadic.VariStructFunc(variadic.NewIntStrUct(1), variadic.NewIntStrUct(2), variadic.NewIntStrUct(3))
print("Variadic Struct s(1)+s(2)+s(3) = %d" % varStructResult)
if isinstance(varResult, int):
print("Type OK")

View File

@ -1,13 +1,6 @@
package variadic
func VariFunc(vargs ...int) int{
total := 0
for _, num := range vargs {
total += num
}
return total
}
/////////////// Non Variadic //////////////
func NonVariFunc(arg1 int, arg2 []int, arg3 int) int{
total := arg1
for _, num := range arg2 {
@ -17,3 +10,33 @@ func NonVariFunc(arg1 int, arg2 []int, arg3 int) int{
return total
}
/////////////// Variadic Over Int //////////////
func VariFunc(vargs ...int) int{
total := 0
for _, num := range vargs {
total += num
}
return total
}
/////////////// Variadic Over Struct //////////////
type IntStrUct struct {
p int
}
func NewIntStrUct(n int) IntStrUct {
return IntStrUct {
p:n,
}
}
func VariStructFunc(vargs ...IntStrUct) int{
total := 0
for _, inst := range vargs {
total += inst.p
}
return total
}
/////////////// Variadic Over Interface //////////////

View File

@ -322,7 +322,11 @@ if __err != nil {
// To support variadic args, we add *args at the end.
if fsym.isVariadic && i == len(args)-1 {
g.pywrap.Printf("%s = go.Slice_int(args)\n", anm)
packagePrefix := ""
if arg.sym.gopkg.Name() != fsym.pkg.Name() {
packagePrefix = arg.sym.gopkg.Name() + "."
}
g.pywrap.Printf("%s = %s%s(args)\n", anm, packagePrefix, arg.sym.id)
}
}

View File

@ -820,8 +820,9 @@ func TestBindVariadic(t *testing.T) {
lang: features[path],
cmd: "build",
extras: nil,
want: []byte(`Variadic 1+2+3+4+5 = 15
NonVariadic 1+[2+3+4]+5 = 15
want: []byte(`NonVariadic 1+[2+3+4]+5 = 15
Variadic 1+2+3+4+5 = 15
Variadic Struct s(1)+s(2)+s(3) = 6
Type OK
`),
})