Generic type alias with type conversion

179 views
Skip to first unread message

hey...@gmail.com

unread,
Nov 3, 2024, 2:40:42 AMNov 3
to golang-nuts
Not sure generic type alias is the solution, but what I'm trying to do is I have a type like this

type Map[K comparable, V any] struct { m map[K]V }

and I want it to implement json.Unmarshaller to decode json objects, when K's underlying type is string:

type omap[K ~string, V any] = Map[K, V]
func (m *omap[K, V]) UnmarshalJSON(data []byte) error {
  k := "a"
  v :=1
  m[K(k)] = v
}

Go 1.23 with the aliastypeparams flag enabled, currently complains "cannot convert k (variable of type string) to type K".

I wonder if using alias this way is wrong and if there exists another solution?

Ian Lance Taylor

unread,
Nov 4, 2024, 12:47:30 AMNov 4
to hey...@gmail.com, golang-nuts
A type alias is just an alias. When you declare a method on the alias
map, you are declaring a method on the type Map. That means that the
~string in the omap definition is ignored for the method definition;
all that matters is the comparable constraint in the Map definition.
You can't in general convert a string to a comparable type.

Ian

hey...@gmail.com

unread,
Nov 4, 2024, 5:39:51 AMNov 4
to golang-nuts
Thanks for the reply.

Does that mean it's currently not possible to conditionally implement a method based on type parameters? And the solution should probably be that I use CanConvert from reflect do that at runtime?

tapi...@gmail.com

unread,
Nov 4, 2024, 7:10:06 AMNov 4
to golang-nuts
Why do you have to use alias here? What is the problem if omap is not an alias?

tapi...@gmail.com

unread,
Nov 4, 2024, 7:11:12 AMNov 4
to golang-nuts
On Monday, November 4, 2024 at 1:47:30 PM UTC+8 Ian Lance Taylor wrote:
There is a little weirdness here. Maybe delcaring method on geneirc aliases should be forbidden.

Ian Lance Taylor

unread,
Nov 4, 2024, 1:46:06 PMNov 4
to hey...@gmail.com, golang-nuts
On Mon, Nov 4, 2024 at 2:40 AM hey...@gmail.com <hey...@gmail.com> wrote:
>
> Does that mean it's currently not possible to conditionally implement a method based on type parameters?

I think that is https://go.dev/issue/65394.

Ian


> On Monday, November 4, 2024 at 1:47:30 PM UTC+8 Ian Lance Taylor wrote:
>>
>> On Sun, Nov 3, 2024 at 12:41 AM hey...@gmail.com <hey...@gmail.com> wrote:
>> >
>> > Not sure generic type alias is the solution, but what I'm trying to do is I have a type like this
>> >
>> > type Map[K comparable, V any] struct { m map[K]V }
>> >
>> > and I want it to implement json.Unmarshaller to decode json objects, when K's underlying type is string:
>> >
>> > type omap[K ~string, V any] = Map[K, V]
>> > func (m *omap[K, V]) UnmarshalJSON(data []byte) error {
>> > k := "a"
>> > v :=1
>> > m[K(k)] = v
>> > }
>> >
>> > Go 1.23 with the aliastypeparams flag enabled, currently complains "cannot convert k (variable of type string) to type K".
>> >
>> > I wonder if using alias this way is wrong and if there exists another solution?
>>
>> A type alias is just an alias. When you declare a method on the alias
>> map, you are declaring a method on the type Map. That means that the
>> ~string in the omap definition is ignored for the method definition;
>> all that matters is the comparable constraint in the Map definition.
>> You can't in general convert a string to a comparable type.
>>
>> 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 visit https://groups.google.com/d/msgid/golang-nuts/3b54013b-8d8d-4eb8-9e3b-77b50de0d3ebn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages