Dave.
chan time.Duration
to
chan int64
And that seems should be possible.
--Petar
> And that seems should be possible.
Perhaps, but it's still a bit contrived. There's nothing in the time
package that gives you a chan time.Duration either.
Dave.
You have a function like
func F() chan T
where T is a type defined as
type T Q (or maybe as type struct { Q })
for Q some other type.
You should be able (it seems) to convert
chan T to chan Q
since you are downgrading the type (meaning) of the channel messages,
and there is nothing logically wrong with that.
I was just noting that if you don't allow this. You would have
to start a new goroutine that converts messages upon reception
and sends them to a different channel. Seems clunky.
P
-rob
> You can't?
>
> http://play.golang.org/p/aEkJIMKxUg
I admit to being surprised by that; I didn't think that would work.
So let me try again: You can't convert a []int to a []X. http://play.golang.org/p/PenyWj0pMe
-rob
"A non-constant value x can be converted to type T in any of these cases: [...]
x's type and T are unnamed pointer types and their pointer base types
have identical underlying types."
it can be a useful technique - here's an example from the Go source:
http://weekly.golang.org/src/pkg/sync/rwmutex.go#L87
although looking at that code, i think the following is probably clearer,
and with struct equality allowed, it doesn't even change the
comparison semantics:
func (rw *RWMutex) RLocker() Locker {
return rlocker{rw}
}
type rlocker struct {
m *RWMutex
}
func (r rlocker) Lock() { r.m.RLock() }
func (r rlocker) Unlock() { r.m.RUnlock() }
I was just noting that if you don't allow this. You would have
to start a new goroutine that converts messages upon reception
and sends them to a different channel. Seems clunky.
I admit to being surprised by that; I didn't think that would work.
type X int
then you can't convert
[]X to []int
or
chan X to chan int
using the current compiler. Agreed.
My question was more of a language design question:
Why shouldn't we be able to?
If you were to allow it, would that break anything else in the language design?
In the case of chan X to chan int, you could take the clumsy approach
and implement a go routine which proxies between the two channels. And then
argue that it is the compiler optimizer's job to get rid of one of the
goroutines.
But then this argument would not apply to the case []X to []int. Since the only
way to make the cast within the language (without using unsafe)
would be to copy the array, or to
convert the elements one at a time, when you use them individually.
Both of which, seem "clunky" as well.
P
We allow the conversion in two cases:
1. When T and U have identical definitions, can convert T <-> U.
2. When T and U have identical definitions, can convert *T <-> *U.
Both of these are allowed so that the method set can be
controlled, as Rob pointed out. If not for method sets,
even those would not be allowed.
In general I think a better solution to your specific
problem is to make the code agree on what to call
the type.
Russ
Both of these are allowed so that the method set can be
controlled, as Rob pointed out. If not for method sets,
even those would not be allowed.