Note that this would still accept numeric literals:

    w.At(4)

If you want to avoid this, you can wrap the index type in a struct:

type Index struct {
    I int
}

Quoting Ian Denhardt (2018-10-07 13:20:48)
> Yeah, there's no way to do this as such. One thing you could possibly do
> instead is define a wrapper around the type and use methods:
>
> type MyWrapper struct {
>     theSlice []Elem
> }
>
> type Index int
>
> func(w MyWrapper) At(i Index) Elem {
>     return w.theSlice[int(i)]
> }
>
> func(w MyWrapper) SetAt(i Index, v Elem) {
>     w.theSlice[int(i)] = v
> }
>
> Quoting jake6...@gmail.com (2018-10-07 09:29:23)
> >    I'm gonna say no.
> >    On Saturday, October 6, 2018 at 5:07:33 PM UTC-4, Steve Roth wrote:
> >
> >    Is there a way to declare an array (or slice) that is indexed by a type
> >    derived from int, rather than indexed by int?�  My intent is to have
> >    the compiler complain if I index the array/slice with a generic int
> >    rather than the specific int-derived type I'm supposed to use.�  This
> >    will help verify correct usage of the array.�  I didn't see any way to
> >    do this in the language spec, but hopefully I just missed it?
> >    Thanks,
> >    Steve
> >
> >    --
> >    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 [1]golang-nuts+unsubscr...@googlegroups.com.
> >    For more options, visit [2]https://groups.google.com/d/optout.
> >
> > Verweise
> >
> >    1. mailto:golang-nuts+unsubscr...@googlegroups.com
> >    2. https://groups.google.com/d/optout

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to