Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:49:20+00:00 2026-06-18T02:49:20+00:00

I have two different event types implementing the same interface: interface InputEvent { }

  • 0

I have two different event types implementing the same interface:

interface InputEvent { }

struct KeyboardEvent : InputEvent { }
struct MouseEvent : InputEvent { }

I have two streams, one of each event type:

IObservable<KeyboardEvent> KeyboardStream;
IObservable<MouseEvent> MouseStream;

I would like to introduce a merged IObservable<InputEvent> stream of both. At first I hoped that the compiler would automatically detect the base class:

IObservable<InputEvent> Merged = Observable.Merge(KeyboardStream, MouseStream);

No luck there, so I tried being explicit:

IObservable<InputEvent> Merged = Observable.Merge<InputEvent>(KeyboardStream, MouseStream);

Nope, compiler still doesn’t get the hint. So I cast each explicitly:

IObservable<InputEvent> Merged = Observable.Merge<InputEvent>((IObservable<InputEvent>)KeyboardStream, (IObservable<InputEvent>)MouseStream);

Yuck. And it still fails at runtime with a cast fail. I guess this is something to do with covariance (I still don’t fully get that on the first try…) so I’ll do what I would do with IEnumerable, use .Cast<T>():

IObservable<InputEvent> Merged = Observable.Merge<InputEvent>(KeyboardStream.Cast<InputEvent>(), MouseStream.Cast<InputEvent>());

Now the compiler tells me that .Cast<T>() is only defined for IObservable<Object>… What? That seems like a pretty inconvenient and unnecessary restraint.

Finally I try a simple select:

IObservable<InputEvent> Merged = Observable.Merge(KeyboardStream.Select(i => (InputEvent)i), MouseStream.Select(i => (InputEvent)i));

Success at last! It works and I can create my own simple extension method from it. However, the built-in .Cast<T>() and .OfType<T>() operators leave a pretty bad taste in my mouth. So my question is: why can’t I use the built-in .Cast<T>() extension on any observable other than Object which is pretty much redundant? Is it a covariance issue? An oversight in the Rx spec? A deliberate design decision?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:49:21+00:00Added an answer on June 18, 2026 at 2:49 am

    The issue here was that my event types were structs and therefore weren’t automatically boxed to Object. Changing the types to class did the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two different libGL libraries on the same Ubuntu 11.04 machine. One library
I have two different arrays, one with strings and another with ints. I want
I have two different modules that need access to a single file (One will
I have an ObservableCollection<Object> that contains two different types. I want to bind this
I have two types of links that are event-bound. If the clicked-on link has
I have two tables representing two different types of imagery. I am using PostGIS
I have two different buttons on my page. I want them both to be
I have two different web applications running on two different instances of tomcat. I
I have two different-sized monitors, connected together using (I believe) TwinView. I tried System.out.println(Toolkit.getDefaultToolkit().getScreenSize());
I have two different tables, lead and click. I would like to query MySQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.