Reactive Extensions API in depth: Primitives

Download this episode

Download Video


Jeff describes the primitives of IObservable.



Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • User profile image
      Tom Lokhorst

      Does Return call OnNext after someone subscribes, or calls it OnNext during the call to Subscribe? If I call Subscribe, I get back an IDisposable, but do I get the change to call Dispose before the Observable starts pushing values?


      In other words, what if I want this method:

      IObservable<T> ToObservable<T>(this IEnumerable<T> xs)
        return new IObservable<T>()
          IDisposable Subscribe(IObserver<T> obs)
            foreach (var x in xs)
            return someDisposable(); // Don't know how to implement


      Can I unsubscribe from the IObservable before or during the series of OnNext calls?

    • User profile image

      Looks like these definitions


      never :: () -> ⊥
      empty :: () -> ()
      return :: a -> () -> a
      throw :: Exception a => a -> () -> a


      That is,


      T Never<T>()
          while (true);
          return default(T);
      void Empty()
      Func<T> Return<T>(T x)
          return () => x;
      Action Throw<T>(T x) where T: Exception
          return () => { throw x; };




      var s1 = Observable.Never<int>();
      var s2 = Observable.Empty<int>();
      var s3 = Observable.Return<int>(42);
      var s4 = Observable.Throw<int>(new Exception());


      Primitives - Asynchronous


      var a1 = Observable.Start(() => { while (true); });
      var a2 = Observable.Start(() => { });
      var a3 = Observable.Start(() => 42);
      var a4 = Observable.Start(() => { throw new Exception(); });


      Need to try out but that looks about right to me...

    • User profile image
      Tom Lokhorst

      Bottom isn't really a type. I'd say something like:


      never :: Subject a
      empty :: Subject a
      return :: a -> Subject a
      throw :: Exception e => e -> Subject a


      Where of course:


      instance Monad Subject where 

    • User profile image

      If it doesn't terminate, does the type matter, except that it's "bottom" - which should be every type as well?


      The reason I write it like functions is because if we look at an IObservable then it looks like a statement sequence to me, especially after the Beckman interview where I believe Erik showed this connection in that you could model an imperative program of sequences of statements as these things.


      s1; s2; s3;


      The primitives are then ways to model basic ways that a statement can behave. Some statements never return. Some statements are constant assignments (or we could say assignment expressions). Some statements throw exceptions. Some statements never terminate. To Erik the last two would be somewhat similar according to previous answers.


      And so the first functions defined would mimic this behavior in that you could write


      var s1 = Never(); // s1 is bottom, doesn't ever return
      Empty<int>(); // void - we can't assign that
      var s3 = Return(42)(); // s3 = 42
      Throw(new ForFunException("hah hah"))(); // bottom again


      I guess one of the reasons for providing Never out of the box (that is, in the box), is that if you can be more clever about the implementation instead of saying while(true); simply never call OnNext or OnComplete; no reason to make the CPU hot.

    • User profile image
      Wes Dyer

      Good question.


      We have gone back and forth on this issue over time, but I expect in the long run that the user can choose either one since they both have meaningful semantics.

    • User profile image​box

      In Haskell ⊥ has type forall a :: a.


      bottom :: a

      bottom = bottom

    • User profile image

      All Rx combinators have non-blocking subscribe, so OnNext is called on the SynchronizationContext. For Return's case this means that if you subscribe on the thread owning the SynchronizationContext you're guaranteed to be called after Subscribe returns, or if you're calling from a different thread, you're called either during or after your call to subscribe. In the first case, calling Dispose should guarantee not to make the OnNext call, where the latter you might or might not succeed to cancel subscription.

    • User profile image


      I am working on silverlight application.

      I have an error on following code snips

      Please ans .. 


      Observable.Start(() =>
                                  return "Hello There";
                              }).Subscribe(Value => button.Content = Value);


      this code is not working VS2008 with silverlight application.

      It raised error "Specified method is not supported"

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.