Commit 0e2bb62f authored by Russ Cox's avatar Russ Cox

reflect: rename Typeof, NewValue -> TypeOf, ValueOf

R=r, bradfitzgo
CC=golang-dev
https://golang.org/cl/4433066
parent 41342dc7
This diff is collapsed.
...@@ -117,8 +117,8 @@ func DeepEqual(a1, a2 interface{}) bool { ...@@ -117,8 +117,8 @@ func DeepEqual(a1, a2 interface{}) bool {
if a1 == nil || a2 == nil { if a1 == nil || a2 == nil {
return a1 == a2 return a1 == a2
} }
v1 := NewValue(a1) v1 := ValueOf(a1)
v2 := NewValue(a2) v2 := ValueOf(a2)
if v1.Type() != v2.Type() { if v1.Type() != v2.Type() {
return false return false
} }
......
...@@ -20,82 +20,82 @@ func TestImplicitMapConversion(t *testing.T) { ...@@ -20,82 +20,82 @@ func TestImplicitMapConversion(t *testing.T) {
{ {
// direct // direct
m := make(map[int]int) m := make(map[int]int)
mv := NewValue(m) mv := ValueOf(m)
mv.SetMapIndex(NewValue(1), NewValue(2)) mv.SetMapIndex(ValueOf(1), ValueOf(2))
x, ok := m[1] x, ok := m[1]
if x != 2 { if x != 2 {
t.Errorf("#1 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m) t.Errorf("#1 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
} }
if n := mv.MapIndex(NewValue(1)).Interface().(int); n != 2 { if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
t.Errorf("#1 MapIndex(1) = %d", n) t.Errorf("#1 MapIndex(1) = %d", n)
} }
} }
{ {
// convert interface key // convert interface key
m := make(map[interface{}]int) m := make(map[interface{}]int)
mv := NewValue(m) mv := ValueOf(m)
mv.SetMapIndex(NewValue(1), NewValue(2)) mv.SetMapIndex(ValueOf(1), ValueOf(2))
x, ok := m[1] x, ok := m[1]
if x != 2 { if x != 2 {
t.Errorf("#2 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m) t.Errorf("#2 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
} }
if n := mv.MapIndex(NewValue(1)).Interface().(int); n != 2 { if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
t.Errorf("#2 MapIndex(1) = %d", n) t.Errorf("#2 MapIndex(1) = %d", n)
} }
} }
{ {
// convert interface value // convert interface value
m := make(map[int]interface{}) m := make(map[int]interface{})
mv := NewValue(m) mv := ValueOf(m)
mv.SetMapIndex(NewValue(1), NewValue(2)) mv.SetMapIndex(ValueOf(1), ValueOf(2))
x, ok := m[1] x, ok := m[1]
if x != 2 { if x != 2 {
t.Errorf("#3 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m) t.Errorf("#3 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
} }
if n := mv.MapIndex(NewValue(1)).Interface().(int); n != 2 { if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
t.Errorf("#3 MapIndex(1) = %d", n) t.Errorf("#3 MapIndex(1) = %d", n)
} }
} }
{ {
// convert both interface key and interface value // convert both interface key and interface value
m := make(map[interface{}]interface{}) m := make(map[interface{}]interface{})
mv := NewValue(m) mv := ValueOf(m)
mv.SetMapIndex(NewValue(1), NewValue(2)) mv.SetMapIndex(ValueOf(1), ValueOf(2))
x, ok := m[1] x, ok := m[1]
if x != 2 { if x != 2 {
t.Errorf("#4 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m) t.Errorf("#4 after SetMapIndex(1,2): %d, %t (map=%v)", x, ok, m)
} }
if n := mv.MapIndex(NewValue(1)).Interface().(int); n != 2 { if n := mv.MapIndex(ValueOf(1)).Interface().(int); n != 2 {
t.Errorf("#4 MapIndex(1) = %d", n) t.Errorf("#4 MapIndex(1) = %d", n)
} }
} }
{ {
// convert both, with non-empty interfaces // convert both, with non-empty interfaces
m := make(map[io.Reader]io.Writer) m := make(map[io.Reader]io.Writer)
mv := NewValue(m) mv := ValueOf(m)
b1 := new(bytes.Buffer) b1 := new(bytes.Buffer)
b2 := new(bytes.Buffer) b2 := new(bytes.Buffer)
mv.SetMapIndex(NewValue(b1), NewValue(b2)) mv.SetMapIndex(ValueOf(b1), ValueOf(b2))
x, ok := m[b1] x, ok := m[b1]
if x != b2 { if x != b2 {
t.Errorf("#5 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x, b2, ok, m) t.Errorf("#5 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x, b2, ok, m)
} }
if p := mv.MapIndex(NewValue(b1)).Elem().Pointer(); p != uintptr(unsafe.Pointer(b2)) { if p := mv.MapIndex(ValueOf(b1)).Elem().Pointer(); p != uintptr(unsafe.Pointer(b2)) {
t.Errorf("#5 MapIndex(b1) = %p want %p", p, b2) t.Errorf("#5 MapIndex(b1) = %p want %p", p, b2)
} }
} }
{ {
// convert channel direction // convert channel direction
m := make(map[<-chan int]chan int) m := make(map[<-chan int]chan int)
mv := NewValue(m) mv := ValueOf(m)
c1 := make(chan int) c1 := make(chan int)
c2 := make(chan int) c2 := make(chan int)
mv.SetMapIndex(NewValue(c1), NewValue(c2)) mv.SetMapIndex(ValueOf(c1), ValueOf(c2))
x, ok := m[c1] x, ok := m[c1]
if x != c2 { if x != c2 {
t.Errorf("#6 after SetMapIndex(c1, c2): %p (!= %p), %t (map=%v)", x, c2, ok, m) t.Errorf("#6 after SetMapIndex(c1, c2): %p (!= %p), %t (map=%v)", x, c2, ok, m)
} }
if p := mv.MapIndex(NewValue(c1)).Pointer(); p != NewValue(c2).Pointer() { if p := mv.MapIndex(ValueOf(c1)).Pointer(); p != ValueOf(c2).Pointer() {
t.Errorf("#6 MapIndex(c1) = %p want %p", p, c2) t.Errorf("#6 MapIndex(c1) = %p want %p", p, c2)
} }
} }
...@@ -106,15 +106,15 @@ func TestImplicitMapConversion(t *testing.T) { ...@@ -106,15 +106,15 @@ func TestImplicitMapConversion(t *testing.T) {
// when we do that though, so MyBuffer is defined // when we do that though, so MyBuffer is defined
// at top level. // at top level.
m := make(map[*MyBuffer]*bytes.Buffer) m := make(map[*MyBuffer]*bytes.Buffer)
mv := NewValue(m) mv := ValueOf(m)
b1 := new(MyBuffer) b1 := new(MyBuffer)
b2 := new(bytes.Buffer) b2 := new(bytes.Buffer)
mv.SetMapIndex(NewValue(b1), NewValue(b2)) mv.SetMapIndex(ValueOf(b1), ValueOf(b2))
x, ok := m[b1] x, ok := m[b1]
if x != b2 { if x != b2 {
t.Errorf("#7 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x, b2, ok, m) t.Errorf("#7 after SetMapIndex(b1, b2): %p (!= %p), %t (map=%v)", x, b2, ok, m)
} }
if p := mv.MapIndex(NewValue(b1)).Pointer(); p != uintptr(unsafe.Pointer(b2)) { if p := mv.MapIndex(ValueOf(b1)).Pointer(); p != uintptr(unsafe.Pointer(b2)) {
t.Errorf("#7 MapIndex(b1) = %p want %p", p, b2) t.Errorf("#7 MapIndex(b1) = %p want %p", p, b2)
} }
} }
...@@ -126,8 +126,8 @@ func TestImplicitSetConversion(t *testing.T) { ...@@ -126,8 +126,8 @@ func TestImplicitSetConversion(t *testing.T) {
// Just make sure conversions are being applied at all. // Just make sure conversions are being applied at all.
var r io.Reader var r io.Reader
b := new(bytes.Buffer) b := new(bytes.Buffer)
rv := NewValue(&r).Elem() rv := ValueOf(&r).Elem()
rv.Set(NewValue(b)) rv.Set(ValueOf(b))
if r != b { if r != b {
t.Errorf("after Set: r=%T(%v)", r, r) t.Errorf("after Set: r=%T(%v)", r, r)
} }
...@@ -136,7 +136,7 @@ func TestImplicitSetConversion(t *testing.T) { ...@@ -136,7 +136,7 @@ func TestImplicitSetConversion(t *testing.T) {
func TestImplicitSendConversion(t *testing.T) { func TestImplicitSendConversion(t *testing.T) {
c := make(chan io.Reader, 10) c := make(chan io.Reader, 10)
b := new(bytes.Buffer) b := new(bytes.Buffer)
NewValue(c).Send(NewValue(b)) ValueOf(c).Send(ValueOf(b))
if bb := <-c; bb != b { if bb := <-c; bb != b {
t.Errorf("Received %p != %p", bb, b) t.Errorf("Received %p != %p", bb, b)
} }
...@@ -144,9 +144,9 @@ func TestImplicitSendConversion(t *testing.T) { ...@@ -144,9 +144,9 @@ func TestImplicitSendConversion(t *testing.T) {
func TestImplicitCallConversion(t *testing.T) { func TestImplicitCallConversion(t *testing.T) {
// Arguments must be assignable to parameter types. // Arguments must be assignable to parameter types.
fv := NewValue(io.WriteString) fv := ValueOf(io.WriteString)
b := new(bytes.Buffer) b := new(bytes.Buffer)
fv.Call([]Value{NewValue(b), NewValue("hello world")}) fv.Call([]Value{ValueOf(b), ValueOf("hello world")})
if b.String() != "hello world" { if b.String() != "hello world" {
t.Errorf("After call: string=%q want %q", b.String(), "hello world") t.Errorf("After call: string=%q want %q", b.String(), "hello world")
} }
...@@ -155,9 +155,9 @@ func TestImplicitCallConversion(t *testing.T) { ...@@ -155,9 +155,9 @@ func TestImplicitCallConversion(t *testing.T) {
func TestImplicitAppendConversion(t *testing.T) { func TestImplicitAppendConversion(t *testing.T) {
// Arguments must be assignable to the slice's element type. // Arguments must be assignable to the slice's element type.
s := []io.Reader{} s := []io.Reader{}
sv := NewValue(&s).Elem() sv := ValueOf(&s).Elem()
b := new(bytes.Buffer) b := new(bytes.Buffer)
sv.Set(Append(sv, NewValue(b))) sv.Set(Append(sv, ValueOf(b)))
if len(s) != 1 || s[0] != b { if len(s) != 1 || s[0] != b {
t.Errorf("after append: s=%v want [%p]", s, b) t.Errorf("after append: s=%v want [%p]", s, b)
} }
...@@ -176,8 +176,8 @@ var implementsTests = []struct { ...@@ -176,8 +176,8 @@ var implementsTests = []struct {
func TestImplements(t *testing.T) { func TestImplements(t *testing.T) {
for _, tt := range implementsTests { for _, tt := range implementsTests {
xv := Typeof(tt.x).Elem() xv := TypeOf(tt.x).Elem()
xt := Typeof(tt.t).Elem() xt := TypeOf(tt.t).Elem()
if b := xv.Implements(xt); b != tt.b { if b := xv.Implements(xt); b != tt.b {
t.Errorf("(%s).Implements(%s) = %v, want %v", xv.String(), xt.String(), b, tt.b) t.Errorf("(%s).Implements(%s) = %v, want %v", xv.String(), xt.String(), b, tt.b)
} }
...@@ -202,8 +202,8 @@ type IntPtr1 *int ...@@ -202,8 +202,8 @@ type IntPtr1 *int
func TestAssignableTo(t *testing.T) { func TestAssignableTo(t *testing.T) {
for _, tt := range append(assignableTests, implementsTests...) { for _, tt := range append(assignableTests, implementsTests...) {
xv := Typeof(tt.x).Elem() xv := TypeOf(tt.x).Elem()
xt := Typeof(tt.t).Elem() xt := TypeOf(tt.t).Elem()
if b := xv.AssignableTo(xt); b != tt.b { if b := xv.AssignableTo(xt); b != tt.b {
t.Errorf("(%s).AssignableTo(%s) = %v, want %v", xv.String(), xt.String(), b, tt.b) t.Errorf("(%s).AssignableTo(%s) = %v, want %v", xv.String(), xt.String(), b, tt.b)
} }
......
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
// Package reflect implements run-time reflection, allowing a program to // Package reflect implements run-time reflection, allowing a program to
// manipulate objects with arbitrary types. The typical use is to take a value // manipulate objects with arbitrary types. The typical use is to take a value
// with static type interface{} and extract its dynamic type information by // with static type interface{} and extract its dynamic type information by
// calling Typeof, which returns a Type. // calling TypeOf, which returns a Type.
// //
// A call to NewValue returns a Value representing the run-time data. // A call to ValueOf returns a Value representing the run-time data.
// Zero takes a Type and returns a Value representing a zero value // Zero takes a Type and returns a Value representing a zero value
// for that type. // for that type.
package reflect package reflect
...@@ -808,8 +808,8 @@ func toType(p *runtime.Type) Type { ...@@ -808,8 +808,8 @@ func toType(p *runtime.Type) Type {
return toCommonType(p).toType() return toCommonType(p).toType()
} }
// Typeof returns the reflection Type of the value in the interface{}. // TypeOf returns the reflection Type of the value in the interface{}.
func Typeof(i interface{}) Type { func TypeOf(i interface{}) Type {
eface := *(*emptyInterface)(unsafe.Pointer(&i)) eface := *(*emptyInterface)(unsafe.Pointer(&i))
return toType(eface.typ) return toType(eface.typ)
} }
......
...@@ -1611,9 +1611,9 @@ func Indirect(v Value) Value { ...@@ -1611,9 +1611,9 @@ func Indirect(v Value) Value {
return v.Elem() return v.Elem()
} }
// NewValue returns a new Value initialized to the concrete value // ValueOf returns a new Value initialized to the concrete value
// stored in the interface i. NewValue(nil) returns the zero Value. // stored in the interface i. ValueOf(nil) returns the zero Value.
func NewValue(i interface{}) Value { func ValueOf(i interface{}) Value {
if i == nil { if i == nil {
return Value{} return Value{}
} }
...@@ -1626,7 +1626,7 @@ func NewValue(i interface{}) Value { ...@@ -1626,7 +1626,7 @@ func NewValue(i interface{}) Value {
// Zero returns a Value representing a zero value for the specified type. // Zero returns a Value representing a zero value for the specified type.
// The result is different from the zero value of the Value struct, // The result is different from the zero value of the Value struct,
// which represents no value at all. // which represents no value at all.
// For example, Zero(Typeof(42)) returns a Value with Kind Int and value 0. // For example, Zero(TypeOf(42)) returns a Value with Kind Int and value 0.
func Zero(typ Type) Value { func Zero(typ Type) Value {
if typ == nil { if typ == nil {
panic("reflect: Zero(nil)") panic("reflect: Zero(nil)")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment