Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error when changing type of a var from int to float64 #1120

Closed
serge-hulne opened this issue Mar 22, 2022 · 2 comments
Closed

Getting error when changing type of a var from int to float64 #1120

serge-hulne opened this issue Mar 22, 2022 · 2 comments
Labels
Milestone

Comments

@serge-hulne
Copy link

The following program sample.gop triggers an unexpected result

// add a sample
package main

import (
	"fmt"
	"math"
)

//type vector []int
type vector []float64

func main() {
	// Declaring two vectors
	var x vector = [1, 0, 1]
	var y vector = [0, 1, 0]

	// Sum of two vectors
	println(x + y)

	// Scalar product of two vectors
	println(x * y)

	// Module of a vector
	println(mod(x))
}

func (a vector) + (b vector) vector {
	var v vector
	for k, _ <- a {
		v = append(v, a[k]+b[k])
	}
	return v
}

func (a vector) * (b vector) int {
	s := 0
	for k, _ <- a {
		s += a[k] * b[k]
	}
	return s
}

func mod(v vector) float64 {
	return math.Sqrt(float64(v * v))
}

Expected result

[1 1 1]
0
1.4142135623730951

Got

panic: gox.fatalMsg("don't call End(), please use EndInit() instead") [recovered]
	panic: gox.fatalMsg("don't call End(), please use EndInit() instead")

goroutine 1 [running]:
github.com/goplus/gop/cl.(*pkgCtx).handleRecover(...)
	/Users/sergehulne/gop/cl/compile.go:347
github.com/goplus/gop/cl.(*pkgCtx).loadSymbol.func1()
	/Users/sergehulne/gop/cl/compile.go:325 +0x19c
panic({0x10246ee00, 0x1400040a7a0})
	/usr/local/go/src/runtime/panic.go:838 +0x204
github.com/goplus/gox.fatal(...)
	/Users/sergehulne/go/bin/pkg/mod/github.com/goplus/gox@v1.9.7/package.go:50
github.com/goplus/gox.(*VarDecl).End(0x40?, 0x0?)
	/Users/sergehulne/go/bin/pkg/mod/github.com/goplus/gox@v1.9.7/type_var_and_const.go:140 +0x3c
github.com/goplus/gox.(*CodeBuilder).End(0x14000292048)
	/Users/sergehulne/go/bin/pkg/mod/github.com/goplus/gox@v1.9.7/codebuild.go:2336 +0x17c
github.com/goplus/gop/cl.loadFuncBody(0x1400037bb00, 0x41?, 0x14000107200)
	/Users/sergehulne/gop/cl/compile.go:810 +0x64
github.com/goplus/gop/cl.loadFunc(0x1400037bb00, 0x0, 0x14000107230)
	/Users/sergehulne/gop/cl/compile.go:751 +0x604
github.com/goplus/gop/cl.preloadFile.func2()
	/Users/sergehulne/gop/cl/compile.go:593 +0x7c
github.com/goplus/gop/cl.(*baseLoader).load(0x1024834c0?)
	/Users/sergehulne/gop/cl/compile.go:170 +0x28
github.com/goplus/gop/cl.(*pkgCtx).loadSymbol(0x1400006e1e0, {0x14000122734, 0x4})
	/Users/sergehulne/gop/cl/compile.go:335 +0x15c
github.com/goplus/gop/cl.loadFile(0x1400006e1e0, 0x1400006e1e0?)
	/Users/sergehulne/gop/cl/compile.go:447 +0xbc
github.com/goplus/gop/cl.NewPackage({0x0, 0x0}, 0x140001078c0, 0x14000177ca8)
	/Users/sergehulne/gop/cl/compile.go:392 +0x4b8
github.com/goplus/gop/x/gopproj.(*gopFiles).GenGo(0x140001342e0, {0x1400016c080, 0x40}, {0x1400011a420, 0x21})
	/Users/sergehulne/gop/x/gopproj/gopfiles.go:134 +0x2dc
github.com/goplus/gop/x/gopproj.(*Context).GoCommand(0x140001061a0?, {0x1023d6ee6, 0x3}, 0x140001329a0)
	/Users/sergehulne/gop/x/gopproj/projctx.go:94 +0x1d4
github.com/goplus/gop/cmd/internal/run.gopRun({0x140001061a0?, 0x140001061a0?, 0x1400013de78?})
	/Users/sergehulne/gop/cmd/internal/run/run.go:105 +0x2bc
github.com/goplus/gop/cmd/internal/run.runCmd(0x1400011e120?, {0x140001061a0?, 0x0?, 0x12961eec8?})
	/Users/sergehulne/gop/cmd/internal/run/run.go:63 +0xc8
main.main()
	/Users/sergehulne/gop/cmd/gop/main.go:108 +0x518

Gop Version

gop v1.1.0-alpha1-104-g353daa5c darwin/arm64

Additional Notes

The error appears when I change int to float64 in the definition of vector:

//type vector []int
type vector []float64

@xushiwei xushiwei added the bug label Mar 26, 2022
@xushiwei xushiwei added this to the Go+ v1.1 milestone Mar 26, 2022
@xushiwei xushiwei modified the milestones: Go+ v1.1, Go+ v1.2 May 22, 2022
visualfc added a commit to visualfc/gop that referenced this issue Jun 3, 2022
@visualfc
Copy link
Member

visualfc commented Jun 3, 2022

sample.gop

must change func (a vector) * (b vector) int

func (a vector) * (b vector) float64 {
	var s float64
	for k, _ <- a {
		s += a[k] * b[k]
	}
	return s
}

@serge-hulne
Copy link
Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants