edgent-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dale LaBossiere <dml.apa...@gmail.com>
Subject Re: Streams and tuple type hierarchies - TStream.cast()?
Date Tue, 28 Jun 2016 18:32:43 GMT

> On Jun 28, 2016, at 1:25 PM, Dan Debrunner <djd@debrunners.com> wrote:
> On 6/28/2016 7:06 AM, Dale LaBossiere wrote:
>> The union() gets a compilation error because TStream<Derived*> doesn't extend
>> TStream<Base>.  Std java generics stuff.    A straightforward cast doesn't
>> it requires a bit more convoluted casting.
> I wonder if the signature of the union  should change in someway, or an overload of the

perhaps such as… ?
And it seems like the issue is larger than just union().
Related to the last question / answer, if there are both base-analytics and derived analytics
(and there was no union involved):

TStream<Derived> sD = …; // ingest
baseAnalytics(sD);  // still want/need something like:   baseAnalytics(sD.cast(Base.class));

>> 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?

Yup. typo when transcribing, here and below :-)  Sorry for the confusion.

>> 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

Did I miss something?
The actual static method I wrote/played with is (now simplified as I see the cast to <?>
didn’t help):
  static <T extends U,U> TStream<U> cast(TStream<T> s, Class<U> classU)
    TStream<U> u = (TStream<U>) s;
    return u;
Even though T extends U the compiler warns without the suppress.

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

Yes. Imagine there are both base-type analytics and derived-type analytics.

Where does that leave us?

— Dale

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message