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

Unable to create array of set length #43

Closed
sinzin91 opened this issue Sep 23, 2017 · 3 comments · Fixed by #50
Closed

Unable to create array of set length #43

sinzin91 opened this issue Sep 23, 2017 · 3 comments · Fixed by #50

Comments

@sinzin91
Copy link

Trying to create an array of length 3:

[1] go-pry> var z [3]int
=> <nil>
[2] go-pry> z
=> []int(nil)
[3] go-pry> z[0]
Error:  slice index out of range <nil>

Works in gore:

gore> var z [3]int
gore> z
[3]int{
  0,
  0,
  0,
}
gore> z[0]
0
@d4l3k
Copy link
Owner

d4l3k commented Oct 1, 2017

I investigated this and it's an interesting problem. Doesn't seem like there's a way to create actual [3]int types using reflection which is why this is happening. We can only make []int types which then lose some information when passed into make(). Guess we need some way of tracking extra data on types.

@cosmos72
Copy link

cosmos72 commented May 2, 2018

implementing something like this should suffice to create an actual array (not a slice) with reflection:

import "reflect"

func createArrayOfT(length int, elementType reflect.Type) reflect.Value {
  return reflect.New(reflect.ArrayOf(length, elementType)).Elem()
}

@d4l3k d4l3k mentioned this issue May 2, 2018
@d4l3k
Copy link
Owner

d4l3k commented May 2, 2018

Cool, thanks for letting me know about that! I just created a PR with proper support for fixed length arrays.

@d4l3k d4l3k closed this as completed in #50 May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants