map type conversion

394 views
Skip to first unread message

Leonard Mittmann

unread,
Jul 16, 2023, 12:02:03 PM7/16/23
to golang-nuts
Hi everyone, I am wondering if there is efficient way to do the following type conversion (without looping over the map):

var m = map[uint]uint{ /*...*/ }
type uintXXX uint
// this does not work
var m2 map[uintXXX]uintXXX = (map[uintXXX]uintXXX)(m)

Jan Mercl

unread,
Jul 16, 2023, 1:16:06 PM7/16/23
to Leonard Mittmann, golang-nuts
Yes, AFAICT, the language specification does not allow such
conversion. The underlying types of uintXXX and uint are the same, so
a conversion between uintXXX and uint is allowed in certain
situations. However, the underlying types of map[uintXXX]uintXXX and
map[uint]uint and are different and thus the conversion is not
possible. Exact details available at
https://go.dev/ref/spec#Conversions.

A different question is - could the specs allow this particular
conversion? I guess probably yes. If you can think of a backwards
compatible specs change that relaxes the conversion rules and allows
this and does not have any bad side effects, it might be a good idea
to fill a proposal at the issue tracker. It will be considered, if not
already rejected before.

Kurtis Rader

unread,
Jul 16, 2023, 1:59:42 PM7/16/23
to Leonard Mittmann, golang-nuts
It does work (i.e., compiles) if you use a type alias (note the equal sign):

    type uintXXX = uint

Whether it is a good idea to allow it to work using two distinct types that have the same underlying type is debatable. Personally, I think the current behavior (not allowing the conversion) is preferable.

--
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/3051ef4c-718b-4bbd-bac4-b0bf9a414283n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Leonard Mittmann

unread,
Jul 17, 2023, 8:51:17 AM7/17/23
to Kurtis Rader, golang-nuts
It does work (i.e., compiles) if you use a type alias (note the equal sign)
I can't use a type alias as I need a custom MarshalText-method.

Yes, AFAICT, the language specification does not allow such
conversion.
Yes, but my thought was that it might be possible to do an unsafe conversion as the underlying types are the same. I just have no idea how.

MARTIN RODRIGUEZ-DUQUE (geimel)

unread,
Jul 17, 2023, 9:28:09 AM7/17/23
to golang-nuts

Jakob Borg

unread,
Jul 18, 2023, 3:22:33 AM7/18/23
to Leonard Mittmann, golang-nuts
On 17 Jul 2023, at 14:50, Leonard Mittmann <leonard....@gmail.com> wrote:

Yes, but my thought was that it might be possible to do an unsafe conversion as the underlying types are the same. I just have no idea how.


The unsafe conversion appears to work, but it’s unsafe and not obvious to me that this is guaranteed to work. It’s allowed to convert one pointer to another if they have an “equivalent memory layout”, and I would guess that two map types with equivalent underlying types would share memory layout, but I don’t think the specs say anything about map memory layouts one way or the other.

//jb

Nigel van Keulen

unread,
Jul 26, 2023, 12:59:38 PM7/26/23
to golang-nuts
Maybe my question could be of interest in this topic. I am not sure, and haven't tested if it works with maps, and I am also not sure if it is supposed to work / will work consistently. 
Op dinsdag 18 juli 2023 om 09:22:33 UTC+2 schreef Jakob Borg:
Reply all
Reply to author
Forward
0 new messages