reflect: add a secret method to ArrayOrSliceType.

It was observed that the interface was generic enough
that several other types implemented it too.

Fixes #1530.

R=rsc
CC=golang-dev
https://golang.org/cl/4169063
This commit is contained in:
Rob Pike 2011-02-22 09:21:50 -08:00
parent 1e73fed888
commit 795ff00df0

View File

@ -163,6 +163,10 @@ type SliceType struct {
elem *runtime.Type elem *runtime.Type
} }
// arrayOrSliceType is an unexported method that guarantees only
// arrays and slices implement ArrayOrSliceType.
func (*SliceType) arrayOrSliceType() {}
// Struct field // Struct field
type structField struct { type structField struct {
name *string name *string
@ -397,6 +401,10 @@ func (t *ArrayType) Len() int { return int(t.len) }
// Elem returns the type of the array's elements. // Elem returns the type of the array's elements.
func (t *ArrayType) Elem() Type { return toType(*t.elem) } func (t *ArrayType) Elem() Type { return toType(*t.elem) }
// arrayOrSliceType is an unexported method that guarantees only
// arrays and slices implement ArrayOrSliceType.
func (*ArrayType) arrayOrSliceType() {}
// Dir returns the channel direction. // Dir returns the channel direction.
func (t *ChanType) Dir() ChanDir { return ChanDir(t.dir) } func (t *ChanType) Dir() ChanDir { return ChanDir(t.dir) }
@ -675,6 +683,7 @@ func toType(i interface{}) Type {
type ArrayOrSliceType interface { type ArrayOrSliceType interface {
Type Type
Elem() Type Elem() Type
arrayOrSliceType() // Guarantees only Array and Slice implement this interface.
} }
// Typeof returns the reflection Type of the value in the interface{}. // Typeof returns the reflection Type of the value in the interface{}.