Good god! Henry, you are right! `type Symbol = byte` IS the solution!
Also many thanks for Alex for pointing to issue 71183 
<https://github.com/golang/go/issues/71183>!

Issue 71183 described a decade old discussion about this problem, which 
explains why I have the impression this has been brought up before.
I believe at least for most cases, Henry's approach above is the correct 
solution, and this truly needs to be documented somewhere for further 
reference.
I will be commented on issue 71183 to bring wider awareness to Henry's 
solution.

Many thanks again to both!

On Saturday, December 20, 2025 at 1:48:21 PM UTC+8 Henry wrote:

> You can if you define your type as follow:
>
> ```
> type Symbol = byte
> ```
>
> On Saturday, December 20, 2025 at 9:23:30 AM UTC+7 [email protected] 
> wrote:
>
>> Hi fellow Gophers
>>
>> I'd like to propose that we allow type casting between `[]T` and 
>> `[]U`, when the underlying types `T` and `U` are identical.
>>
>> For example:
>>
>> ```
>> type Symbol byte
>>
>> func Index(a, b []Symbol) int {
>>         return bytes.Index(a, b)
>> }
>> ```
>>
>> Currently, the above code does not compile with the error:
>>
>> `cannot use a (variable of type []Symbol) as []byte value in argument to 
>> bytes.Index`
>>
>> This proposal suggests to make this allowed since `Symbol` is exactly 
>> identical to `byte`.
>>
>> I am aware this has been proposed before, but was turned down since real 
>> world needs at that time were minimal.
>> However, I now have a real need 
>> <https://github.com/fumin/nag/blob/master/nag.go#L774> in my code for 
>> this feature.
>> Alternatively, if this kind of slice type conversion is truly disallowed, 
>> can anyone suggest a better way of writing the above function that 
>> maintains its readability and performance?
>>
>> Thanks
>>
>

-- 
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/9b826c32-e2ca-4ee9-9b5a-dcd5725f1a55n%40googlegroups.com.

Reply via email to