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

proposal: Go 2: integer array <-> integer type conversion #37658

Closed
jfcg opened this issue Mar 4, 2020 · 11 comments
Closed

proposal: Go 2: integer array <-> integer type conversion #37658

jfcg opened this issue Mar 4, 2020 · 11 comments
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@jfcg
Copy link

jfcg commented Mar 4, 2020

Integer types are convertable to each other. I propose to extend it to arrays:

a := [2]uint16{1, 2}
b := int32(a)
c := [4]byte(a)

d := uint32(65537)
e := [2]int16(d)

This will allow to see:

  • sub-bytes or sub-words of a larger integer variable
  • bytes/words in an array as a larger integer

without unsafe.Pointer tricks.

@gopherbot gopherbot added this to the Proposal milestone Mar 4, 2020
@ghost
Copy link

ghost commented Mar 4, 2020

What should the endianness of the converted value be?

@ianlancetaylor ianlancetaylor added v2 An incompatible library change LanguageChange Suggested changes to the Go language labels Mar 4, 2020
@ianlancetaylor
Copy link
Contributor

For language change proposals, please fill out the template at https://go.googlesource.com/proposal/+/refs/heads/master/go2-language-changes.md .

When you are done, please reply to the issue with @gopherbot please remove label WaitingForInfo.

Thanks!

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 4, 2020
@gopherbot gopherbot added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 4, 2020
@josharian
Copy link
Contributor

This can be done now using encoding/binary. The compiler generates very good code for these routines, equivalent (I strongly suspect) to what a built-in conversion would do. It would be good to explain why encoding/binary is insufficient here.

@jfcg
Copy link
Author

jfcg commented Mar 4, 2020

  • I am experienced Go programmer.
  • I have extensive experience with C, C++, Python.
  • This change enhances type conversions and eliminates some uses of unsafe.Pointer (a more advanced feature) or shift & convert, so I believe this change slightly extends what novice programmers can do and it can improve Go learning experience.
  • Afaik this feature was not proposed before.
  • All Go users benefit from this with extended type conversions.
  • This proposal introduces a) integer array <-> integer array b) integer array <-> integer type conversions.
  • The language spec would extend with the proposed conversions.
  • This change is backward compatible.
  • I believe implementation cost is minimal.
  • Compile time cost is similar to integer <-> integer conversion.
  • There is no run time cost.
  • Like a uint64(int64) conversion, this change is a unique feature and orthogonal with other features in Go.
  • This change is not a performance improvement, just a convenient type conversion.

@gopherbot please remove label WaitingForInfo

@gopherbot gopherbot removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 4, 2020
@jfcg
Copy link
Author

jfcg commented Mar 4, 2020

What should the endianness of the converted value be?

Whatever the target's endianness is at compile time. It should behave like an unsafe.Pointer conversion.

@josharian
Copy link
Contributor

Whatever the target's endianness is at compile time.

This was discussed indirectly at #35398

@jfcg
Copy link
Author

jfcg commented Mar 4, 2020

Ok, so because of endianness:

a := uint16(258)
b := [2]byte(a)

c := byte(a) // always 2
d := b[0]    // not well defined

right? We cant just think like unsafe.Pointer conversion.

How about we make it well-defined. We define the conversion such that b[0]=c=2 and b[1]=1 is always true and consistent across platforms. What do you think?

Josh, encoding/binary doesn't seem very convenient in terms of intuition & readability. It looks more suitable for run-time conversion, whereas this is a compile time feature. Though I am sure the compiler can be made to output efficient assembly for it.

@josharian
Copy link
Contributor

It looks more suitable for run-time conversion, whereas this is a compile time feature.

I’m not sure I understand the distinction here. I believe that are functionally identical. Can you explain more?

@jfcg
Copy link
Author

jfcg commented Mar 5, 2020

sorry yes, runtime/compile time distinction is not accurate since we need byte swap operations on some platforms to get the consistency.

@ianlancetaylor
Copy link
Contributor

We aren't going to intentionally make simple programs (that don't use unsafe) generate different results on different systems.

If we adopt little endian conversions as suggested above, what is the advantage of allowing these conversions? It's always possible to write these conversions out directly. Does this really come up often enough to add a somewhat obscure conversion?

@jfcg
Copy link
Author

jfcg commented Mar 12, 2020

I guess you are right in that:

  • we can live without it
  • shift & convert is pretty common for sub-byte access
  • little-endianizing a variable is too specific a choice (why not big?)

@jfcg jfcg closed this as completed Mar 12, 2020
@golang golang locked and limited conversation to collaborators Mar 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

4 participants