On 6/28/2016 7:06 AM, Dale LaBossiere wrote:
In a real use case there were two different types of sensors that had
a common interface/info to be analyzed.  It took a bit to figure out how to
deal with this.  It certainly wasn't convenient.

Imagine the tuple type hierarchy:
[snip]

The union() gets a compilation error because TStream<Derived*> doesn't extend
TStream<Base>.  Std java generics stuff.    A straightforward cast doesn't work;
it requires a bit more convoluted casting.

I wonder if the signature of the union should change in someway, or an overload of the union.


We could make this much easier for users if we added:
TStream<T>:
    // typesafe cast a TStream<T> to a TStream<U>
    // compile error if U doesn't extend T
    <U extends T> TStream<U> cast(Class<U> classU);

Isn't it the case that T is a sub-class of U, so T extends U?

Then one could write:
TStream<Base> sBase = sDerived1.cast(Base.class).union( 
sDerived2.cast(Base.class) );

Does this seem like the right thing to do?  Alternatives?

Fwiw, the implementation of cast() is trivial once you know what to do:
    <U extends T> TStream<U> cast(Class<U> classU) {
      TStream<?> s = (TStream<?>) this;
      @SuppressWarnings("unchecked")
      TStream<U> u = (TStream<U>) s;
      return u;
    }

This just feels wrong, given that T is a sub-class of U that one has to do unchecked casts.

Any reason why the original streams were just not declared with the base type?

Dan.

Reply via email to