Re: [go-nuts] map type conversion

2023-07-26 Thread Nigel van Keulen
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. 
https://groups.google.com/g/golang-nuts/c/0RqzD2jNADE/m/3Hf14DllBAAJ?utm_medium=email_source=footer
Op dinsdag 18 juli 2023 om 09:22:33 UTC+2 schreef Jakob Borg:

> On 17 Jul 2023, at 14:50, Leonard Mittmann  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.
>
>
> https://go.dev/play/p/ViLnLvInv-1
>
> 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
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/18774f52-1d62-45fb-bdde-6322c0476a23n%40googlegroups.com.


Re: [go-nuts] map type conversion

2023-07-18 Thread 'Jakob Borg' via golang-nuts
On 17 Jul 2023, at 14:50, Leonard Mittmann  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.

https://go.dev/play/p/ViLnLvInv-1

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

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/AA75B829-B48F-4207-8D5F-9B75B2D17FAD%40kastelo.net.


Re: [go-nuts] map type conversion

2023-07-17 Thread Leonard Mittmann
>
> 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.

On Sun, 16 Jul 2023 at 19:59, Kurtis Rader  wrote:

> 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.
>
> On Sun, Jul 16, 2023 at 9:02 AM Leonard Mittmann <
> leonard.mittm...@gmail.com> wrote:
>
>> 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)
>>
>> --
>> 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+unsubscr...@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
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGdsN_rpRzOtokpA%3Dd75Xg8v41D4%3DOQuU8dC2fL3fUbL7Vv%3D7w%40mail.gmail.com.


Re: [go-nuts] map type conversion

2023-07-16 Thread Kurtis Rader
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.

On Sun, Jul 16, 2023 at 9:02 AM Leonard Mittmann 
wrote:

> 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)
>
> --
> 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+unsubscr...@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

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD8-7wM%3D4naS%3DLbLFg2N-%2BJgDi4mMku5B5qAzkY8Px-xbA%40mail.gmail.com.


Re: [go-nuts] map type conversion

2023-07-16 Thread Jan Mercl
On Sun, Jul 16, 2023 at 6:02 PM Leonard Mittmann
 wrote:

> 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)

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.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAA40n-Wit7zW7QUDNJR%2BrujhokRxJ0e74xRFTGXX%2BtoNspVB8Q%40mail.gmail.com.