[generics]The ability to allow different length arrays and slices

175 views
Skip to first unread message

hsoo...@gmail.com

unread,
Jul 15, 2020, 10:58:35 AM7/15/20
to golang-nuts
Right now as I understand it, I would still have to write a different generic function for different length arrays, or one that accepted slices. as per:
// Accept slices of T
func someSliceFunc
(type T)(s []T){ ... }
// Accept a 1 member array of T
func
someArrayFunc(type T)(a [1]T){ ... }
...
// For what ever number of specific length arrays you wish to support.
func someBiggerArrayFunc(type T)(a [???]T){ ... }


It would be nice to indicate the generic function accepts any length of array as well as slices, something like:
// Accept any length of array or slice of T
func
someFunc(type T)(a [...]T){ ... }

Where the [...] indicates the function works with any length array or a slice.

Russell

Axel Wagner

unread,
Jul 15, 2020, 3:14:19 PM7/15/20
to hsoo...@gmail.com, golang-nuts
Why not just accept slices only and expect the caller to add a `[:]`? It's not quite as ergonomic, but should have the same effect and we wouldn't need to add complexity to the design.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f70ef878-622a-4b8e-94ca-40331df866e9o%40googlegroups.com.

benjamin...@gmail.com

unread,
Jul 21, 2020, 12:50:56 PM7/21/20
to golang-nuts
Clearly that's a workaround though.  Like how you can copy a slice of bytes without calling copy by casting it to a string and then back to []bytes.  It gets the job done, but it's weird and not obvious.


On Wednesday, July 15, 2020 at 12:14:19 PM UTC-7, Axel Wagner wrote:
Why not just accept slices only and expect the caller to add a `[:]`? It's not quite as ergonomic, but should have the same effect and we wouldn't need to add complexity to the design.

On Wed, Jul 15, 2020 at 4:58 PM <hsoo...@gmail.com> wrote:
Right now as I understand it, I would still have to write a different generic function for different length arrays, or one that accepted slices. as per:
// Accept slices of T
func someSliceFunc
(type T)(s []T){ ... }
// Accept a 1 member array of T
func
someArrayFunc(type T)(a [1]T){ ... }
...
// For what ever number of specific length arrays you wish to support.
func someBiggerArrayFunc(type T)(a [???]T){ ... }


It would be nice to indicate the generic function accepts any length of array as well as slices, something like:
// Accept any length of array or slice of T
func
someFunc(type T)(a [...]T){ ... }

Where the [...] indicates the function works with any length array or a slice.

Russell

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages