On 01/22, 吴煊 via Tech-talk wrote:
My question is how to use each channel to put value? I've not found any example. I have tried: channels.get(0).put("test"), it's not correct.(error info:The method put(capture#1-of ?) in the type Channel<capture#1-of ?> is not applicable for the arguments (String))
That put method is just going to be awkward to use when the type of the
object it's called on is Channel<?> (i.e., a Channel of some type).
There are some other methods on Channel that you could call without
trouble, but put(T) will be problematic because its argument is of type
T and the object you're calling it on is a Channel of some type, not
necessarily type T. If you have to call that method, the best you can
do is cast the Channel<?> object to the correct type, Channel<String> in
your example:
(Channel<String>(channels.get(0))).put("test");
But that's ugly, and you're throwing away all of the benefits of
generics (e.g., type safety and expressiveness). You'd also have to add
a @SuppressWarnings("unchecked") annotation to the method, or change
the code to a local variable assignment and put the annotation on the
variable, to get rid of the compiler warning.
So, I'd say that you'd be better off not trying to call that put(T)
method like that. Instead, stick to only calling methods on those
channels that you can call in a type-safe way.
If you really need some way to do what you're trying to do, you'd
probably want something added to the ca API to help with that.
If you have to do it yourself, though, you could create your own
container object that could hold Channel instances and that could
provide a get and put method that took a type token. Then, assuming
"channels" was an instance of this container object, your example could
become something like this:
channels.put(0, String.class, "test");
Of course, there are various designs you could come up with.
Lewis