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

Conversions from slice to array pointer #114

Open
vsivsi opened this issue Aug 31, 2021 · 1 comment
Open

Conversions from slice to array pointer #114

vsivsi opened this issue Aug 31, 2021 · 1 comment
Assignees

Comments

@vsivsi
Copy link

vsivsi commented Aug 31, 2021

Go 1.17 introduces a small but useful new language feature:

Converting a slice to an array pointer yields a pointer to the underlying array of the slice. If the length of the slice is less than the length of the array, a run-time panic occurs.

s := make([]byte, 2, 4)
s0 := (*[0]byte)(s)      // s0 != nil
s1 := (*[1]byte)(s[1:])  // &s1[0] == &s[1]
s2 := (*[2]byte)(s)      // &s2[0] == &s[0]
s4 := (*[4]byte)(s)      // panics: len([4]byte) > len(s)

var t []string
t0 := (*[0]string)(t)    // t0 == nil
t1 := (*[1]string)(t)    // panics: len([1]string) > len(t)

u := make([]byte, 0)
u0 = (*[0]byte)(u)       // u0 != nil

Obviously gomacro doesn't yet support this because it's new. E.g.:

gomacro> s := make([]byte, 2, 4)
gomacro> s
[0 0]	// []uint8
gomacro> s0 := (*[2]byte)(s)
repl.go:1:18: cannot convert []uint8 to *[2]uint8: s
@cosmos72 cosmos72 self-assigned this Jan 9, 2022
@cosmos72
Copy link
Owner

cosmos72 commented Jan 9, 2022

Yes, this new language feature should be implemented.
No idea when I will have time for that, though

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

No branches or pull requests

2 participants