This document is a sketch of two interface types that could be added to the draft Type Parameters design to enable conversion and assignment in generic code.
The interface type convertible.To(T)
is implemented by any dynamic type that
can be converted to T
, including T
itself, any type assignable to T
, and
any type that has the same underlying type as T
. A variable of type
convertible.To(T)
may be converted to T
itself. If the variable is the
nil
interface value, the result of the conversion is the zero-value of T
.
If T
is itself an interface type, convertible.To(T)
has the same method set
as T
.
The interface type assignable.To(T)
is implemented by any dynamic type that is
assignable to T
. A variable of the interface type assignable.To(T)
is
assignable to T
and, if the underlying type of T
is a not a
defined type, also to that underlying type. If the variable is nil
, the
value assigned is the zero-value of T
.
If T
is itself an interface type, assignable.To(T)
has the same method set
as T
.
(If T
is not an interface type, a variable of type assignable.To(T)
must
not be assignable to any other defined type whose underlying type is T
— even
if T
itself is not a defined type — because assignable.To(T)
may store
values of other defined types.)
For example, given:
type MyChan <-chan int
type OtherChan <-chan int
var (
a chan int
b <-chan int
c assignable.To(MyChan)
d MyChan
e OtherChan
f assignable.To(<-chan int)
g chan<- int
)
a
is assignable tob
,c
,d
,e
,f
, andg
.b
is assignable toc
,d
,e
, andf
.c
is assignable tob
,d
, andf
(but note
, becausec
could contain a variable of typeMyChan
— which is not assignable toOtherChan
).d
is assignable tob
,c
, andf
.e
is assignable tob
andf
.f
is assignable tob
(but notd
,d
, ore
, becausef
could contain a variable of either typeOtherChan
orMyChan
).g
is assignable to nothing.