[email protected] <[email protected]> writes:
>
> Hi fellow Gophers
Hi
>
> 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 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?
>
I noticed that you used `*(*[]byte)(unsafe.Pointer(&x))` to convert a []Symbol
into a []byte.
I think the only downside of doing this is that the compiler will no longer
help you check whether Symbol and byte are actually the same type.
As long as you add a single line like `_ = byte(Symbol(0))` in the code, you
can let the compiler perform that type check for you.
- Lidong
--
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/74B1FB6A-C7F1-4395-9B0E-1133D0A4905D%40gmail.com.