reflect: add Type.Implements, Type.AssignableTo, Value.CallSlice; make Set match Go

This CL makes reflect require that values be assignable to the target type
in exactly the same places where that is the rule in Go.  It also adds
the Implements and AssignableTo methods so that callers can check
the types themselves so as to avoid a panic.

Before this CL, reflect required strict type identity.

This CL expands Call to accept and correctly marshal arbitrary
argument lists for variadic functions; it introduces CallSlice for use
in the case where the slice for the variadic argument is already known.

Fixes #327.
Fixes #1212.

R=r, dsymonds
CC=golang-dev
https://golang.org/cl/4439058
This commit is contained in:
Russ Cox 2011-04-20 16:24:45 -04:00
parent ec735c4ec8
commit e1ee3b5db6
4 changed files with 576 additions and 68 deletions

View file

@ -5,6 +5,7 @@
package reflect_test
import (
"bytes"
"container/vector"
"fmt"
"io"
@ -1449,3 +1450,20 @@ func TestSlice(t *testing.T) {
t.Errorf("xa.Slice(2, 5) = %v", v)
}
}
func TestVariadic(t *testing.T) {
var b bytes.Buffer
V := NewValue
b.Reset()
V(fmt.Fprintf).Call([]Value{V(&b), V("%s, %d world"), V("hello"), V(42)})
if b.String() != "hello, 42 world" {
t.Errorf("after Fprintf Call: %q != %q", b.String(), "hello 42 world")
}
b.Reset()
V(fmt.Fprintf).CallSlice([]Value{V(&b), V("%s, %d world"), V([]interface{}{"hello", 42})})
if b.String() != "hello, 42 world" {
t.Errorf("after Fprintf CallSlice: %q != %q", b.String(), "hello 42 world")
}
}