rx icon indicating copy to clipboard operation
rx copied to clipboard

How to get reference to underlying object/function of an observer?

Open Robert-M-Muench opened this issue 5 years ago • 1 comments

When using observers, is there a way to access the underlying object implementing the observer?

class myClass {...}
myClass mc = new myClass;
auto observer1 = makeObserver(&mc.myPut);

How can I access/get back mc via observer1?

Since there are several different ways to get an observer (makeObserver which returns an AnonymousObserver struct, inheritance, observerObject) how can I get the pointer/reference to the underlying object/function implementing the functions?

struct TestObserver  {
  void put(int n) { }
  void put(Object obj) { }
}
Observer!int observer = observerObject!int(TestObserver());

How to access the pointer created by TestObserver() call?

The use case is: An instance of myClass can have many observers. These observers are handled around and subscribed to streams etc. Sometimes I need to get back the object an observer belongs to call some functions etc.

Robert-M-Muench avatar Jun 24 '19 17:06 Robert-M-Muench

This works (when _range can be accessed), but the necessary cast doesn't look that nice:

  auto test = new TestObserver;
  auto observer = observerObject!int(test);
  assert(test == cast(ObserverObject!(TestObserver,int))(observer._range));
}

adding

    R object() @property {return _range;}

makes things much nicer.

Robert-M-Muench avatar Jun 30 '19 17:06 Robert-M-Muench