[generics] cannot use generic type list.Element(type TElem) without instantiation

650 views
Skip to first unread message

aurelien...@gmail.com

unread,
Jul 1, 2020, 12:39:25 PM7/1/20
to golang-nuts
Hi,

Is this expected?

import "list" // for 'std' list.go2

func main() {
type intElement list.Element(int)
_ = make(map[int]intElement) // ok: compiles

// nok
_ = make(map[int]list.Element(int)) // cannot use generic type list.Element(type TElem) without instantiation
}

I don't think it is, but in case it is, how would I instantiate a make(map[int]intElement) without having to typedef a generic type before.

PS: i'm using the latest commit at the time of writing in the dev.go2go branch (6cf6bf162c)

Ian Lance Taylor

unread,
Jul 1, 2020, 12:50:07 PM7/1/20
to aurelien...@gmail.com, golang-nuts, Robert Griesemer
On Wed, Jul 1, 2020 at 9:39 AM <aurelien...@gmail.com> wrote:
>
> Is this expected?
>
> import "list" // for 'std' list.go2
>
> func main() {
> type intElement list.Element(int)
> _ = make(map[int]intElement) // ok: compiles
>
> // nok
> _ = make(map[int]list.Element(int)) // cannot use generic type list.Element(type TElem) without instantiation
> }
>
> I don't think it is, but in case it is, how would I instantiate a make(map[int]intElement) without having to typedef a generic type before.

From the parser's perspective the argument to make is an expression,
so in this case you need parentheses to keep this from looking like a
type conversion.

make(map[int](list.Element(int)))

Ian

Andrey T.

unread,
Jul 1, 2020, 11:22:26 PM7/1/20
to golang-nuts
Ian,

continuation question -- does `type intElement list.Element(int)` introduce a new type just like `type myInt int` does? If so, in a previous example, these 2 maps are not of the same type, am I right?

Thank you very much,
  Andrey

On Wednesday, July 1, 2020 at 10:50:07 AM UTC-6, Ian Lance Taylor wrote:

Ian Lance Taylor

unread,
Jul 1, 2020, 11:39:14 PM7/1/20
to Andrey T., golang-nuts
On Wed, Jul 1, 2020 at 8:22 PM Andrey T. <xnow4f...@sneakemail.com> wrote:
>
> continuation question -- does `type intElement list.Element(int)` introduce a new type just like `type myInt int` does? If so, in a previous example, these 2 maps are not of the same type, am I right?

Yes, "type intElement list.Element(int)" introduces a new type. You
can avoid introducing a new type by using a type alias: "type
intElement = list.Element(int)".

Ian


> On Wednesday, July 1, 2020 at 10:50:07 AM UTC-6, Ian Lance Taylor wrote:
>>
>> On Wed, Jul 1, 2020 at 9:39 AM <aurelie...@gmail.com> wrote:
>> >
>> > Is this expected?
>> >
>> > import "list" // for 'std' list.go2
>> >
>> > func main() {
>> > type intElement list.Element(int)
>> > _ = make(map[int]intElement) // ok: compiles
>> >
>> > // nok
>> > _ = make(map[int]list.Element(int)) // cannot use generic type list.Element(type TElem) without instantiation
>> > }
>> >
>> > I don't think it is, but in case it is, how would I instantiate a make(map[int]intElement) without having to typedef a generic type before.
>>
>> From the parser's perspective the argument to make is an expression,
>> so in this case you need parentheses to keep this from looking like a
>> type conversion.
>>
>> make(map[int](list.Element(int)))
>>
>> Ian
>
> --
> 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/651e60c9-323f-481a-8eb1-a15e20e0f68do%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages