On Sat, Feb 5, 2022 at 10:38 AM Marcelo Cantos <marcelo.can...@gmail.com> wrote:
>
> I am porting an int-set type to generics. It holds bits in a block structure 
> with 512 bits per block. At some point, I need to divide by 512:
>
> func block[T constraints.Integer](i T) T {
>     return i / 512
> }
>
> Go 1.18 beta 2 complains: cannot convert 512 (untyped int constant) to T
>
> The problem is that uint8 and int8 cannot represent the number 512. If I 
> build a constraint similar to constraints.Integer that excludes ~uint8 and 
> ~int8, it works fine. Explicit casting is no better, and there is no 
> promotion (e.g., cast i to int and cast the result back to T) that's suitable 
> for all integer types. I would like to shrink the block size for (u)int8, but 
> there's no way to specialize the logic for specific types.
>
> I figured out that I can use i >> 9 instead of i / 512 in this instance, 
> though there will be other cases (divide by non-Po2) where there isn't a 
> simple workaround Is there a general way around this limitation?

It's not clear to me what is supposed to happen for the int8/uint8
case.  It sounds like your block type won't work for int8/uint8.  In
that case it seems like the right approach is to use a constraint that
doesn't permit it, as you suggest.  What other kind of workaround
would be appropriate?

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

Reply via email to