FAQ
In my program I had to write two separate function to concat array of
string vs array of int, e.g.

     func concat(a, b []string) []string {
       ret := make([]string, len(a) + len(b))
       copy(ret, a)
       copy(ret[len(a):], b)
       return ret
     }

     func concat_int(a, b []int) []int {
       ret := make([]int, len(a) + len(b))
       copy(ret, a)
       copy(ret[len(a):], b)
       return ret
     }

Is there no way to create one general function?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jan Mercl at Aug 6, 2013 at 1:40 pm

    On Tue, Aug 6, 2013 at 3:26 PM, TR NS wrote:
    Is there no way to create one general function?
    Use append[0].

       [0]: http://golang.org/ref/spec#Appending_and_copying_slices

    -j

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • TR NS at Aug 6, 2013 at 7:38 pm

    On Tuesday, August 6, 2013 9:39:44 AM UTC-4, Jan Mercl wrote:
    On Tue, Aug 6, 2013 at 3:26 PM, TR NS <tran...@gmail.com <javascript:>>
    wrote:
    Is there no way to create one general function?
    Use append[0].

    [0]: http://golang.org/ref/spec#Appending_and_copying_slices
    Interesting. Thanks for link. (Lots of other good info too!)

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chris dollin at Aug 6, 2013 at 1:44 pm
    On 6 August 2013 14:26, TR NS wrote:

    In my program I had to write two separate function to concat array of
    string vs array of int, e.g.

    func concat(a, b []string) []string {
    ret := make([]string, len(a) + len(b))
    copy(ret, a)
    copy(ret[len(a):], b)
    return ret
    }

    func concat_int(a, b []int) []int {
    ret := make([]int, len(a) + len(b))
    copy(ret, a)
    copy(ret[len(a):], b)
    return ret
    }

    Is there no way to create one general function?
    No (well, you probably could use reflection, but that would be
    a really heavy approach). This is part of the generics issue.

    Instead of calling `concat(sliceA, sliceB)` you could write
    `append(sliceA, sliceB...)`. Now you don't have to write any
    concats at all, which is a win over writing one generic concat.

    Chris

    [Noting that in both cases you have to put the result somewhere.]

    --
    Chris "allusive" Dollin

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • TR NS at Aug 6, 2013 at 2:35 pm

    On Tuesday, August 6, 2013 9:44:18 AM UTC-4, chris dollin wrote:

    Instead of calling `concat(sliceA, sliceB)` you could write
    `append(sliceA, sliceB...)`. Now you don't have to write any
    concats at all, which is a win over writing one generic concat.
    That helps. Thanks!

    Beyond the specific case of concat, are there any plans to address the
    generics thing at all? One function for an array is often complete suitable
    regardless of element type, so it seems like there should be possible.



    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jan Mercl at Aug 6, 2013 at 2:43 pm

    On Tue, Aug 6, 2013 at 4:35 PM, TR NS wrote:
    Beyond the specific case of concat, are there any plans to address the
    generics thing at all? One function for an array is often complete suitable
    regardless of element type, so it seems like there should be possible.
    http://golang.org/doc/faq#generics

    -j

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Chris dollin at Aug 6, 2013 at 2:50 pm

    On 6 August 2013 15:35, TR NS wrote:
    Beyond the specific case of concat, are there any plans to address the
    generics thing at all?

    There's a whole bunch of history of proposals and discusssion and
    so far none of the proposals have passed the rather stringent criteria
    of the core Go team.

    One function for an array is often complete suitable regardless of element
    type, so it seems like there should be possible.
    The code has to be different for various different kinds of arrays, and you
    don't know in advance how many of the different kinds you'll need, so
    even just for copying/concatenating arrays you have to be careful to
    generate
    neither too few nor too many versions, and to not spend an excessive
    amount of (link) time working out how many the right number is.

    The devil is in the details ...

    Chris

    --
    Chris "allusive" Dollin

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Atomly at Aug 6, 2013 at 11:36 pm
    Well, there is a language built-in for this specific case, but if you're
    looking for parametric polymorphism, then you're out of luck.

    You can write ugly reflection code like this sometimes to conquer some
    cases:

    http://golang.org/src/pkg/database/sql/driver/types.go

    :: atomly ::

    [ atomly@atomly.com : www.atomly.com : http://blog.atomly.com/ ...
    [ atomiq records : new york city : +1.347.692.8661 ...
    [ e-mail atomly-news-subscribe@atomly.com for atomly info and updates ...

    On Tue, Aug 6, 2013 at 9:26 AM, TR NS wrote:

    In my program I had to write two separate function to concat array of
    string vs array of int, e.g.

    func concat(a, b []string) []string {
    ret := make([]string, len(a) + len(b))
    copy(ret, a)
    copy(ret[len(a):], b)
    return ret
    }

    func concat_int(a, b []int) []int {
    ret := make([]int, len(a) + len(b))
    copy(ret, a)
    copy(ret[len(a):], b)
    return ret
    }

    Is there no way to create one general function?

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 6, '13 at 1:26p
activeAug 6, '13 at 11:36p
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase