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

Support cutting elements from the slice with append #999

Closed
alexvanin opened this issue May 28, 2020 · 2 comments
Closed

Support cutting elements from the slice with append #999

alexvanin opened this issue May 28, 2020 · 2 comments
Labels
compiler Go smart contract compiler

Comments

@alexvanin
Copy link
Contributor

Sometimes you need to remove element from the slice and one of the convenient ways to that is to use append.

package foo
func Main() int {
	a := []int{0,1,2,3,4,5}
	a = append(a[:2], a[3:]...)
	return len(a)
}

This code does not compile in neo-go with (SUBSTR): can't convert Array to ByteArray message You have to create new slice, which is totally fine, but may be a bit confusing. So it would be nice to support this feature if possible.

@fyrchik
Copy link
Contributor

fyrchik commented Jun 8, 2020

This happens because subslicing is supported only for []byte via SUBSTR instruction.
There is no similar instruction for regular Arrays and the only way to support this is via loops (which can be done manually anyway).

However for your specific case, there is a REMOVE instruction, which can be wrapped in separate builtin in 2.x and possibly invoked directly after #941 .

@fyrchik
Copy link
Contributor

fyrchik commented Jun 10, 2020

Closed via #1021.
Your example:

package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/util"
func Main() int {
	a := []int{0,1,2,3,4,5}
	util.Remove(a, 2)
	return len(a)
}

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

Successfully merging a pull request may close this issue.

2 participants