Coffeehouse Thread

20 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

AsObservable Extension Method

Back to Forum: Coffeehouse
  • User profile image
    Santa's Envoy

    Why isn't there a generic, extension method for delegate types able to transform them (events or not) into Observables? Is it because of some limitation they've (Microsoft) imposed over events or delegate types? Is it because of something about delegate types themselves? I did write some code (pastebin link below) to test it out and, unsurprisingly, it did not work; nor with events, nor with regular delegate type variables. Take a quick look at the code and you'll understand it all.

     

    http://pastebin.com/3ugP7SKW

  • User profile image
    JoshRoss

    I remember seeing something about turning mousemove events into observables.  Does any one remember that?

     

    EDIT: Something like this?

  • User profile image
    Santa's Envoy

    JoshRoss said:

    I remember seeing something about turning mousemove events into observables.  Does any one remember that?

     

    EDIT: Something like this?

    Yeah, but that's extremelly coupled not only to the event itself, but to its name. The approach I'm looking for is a completelly generic way to convert any delegate type into an Observable - not only a specific event or an event with a specific name.

  • User profile image
    JoshRoss

    Santa's Envoy said:
    JoshRoss said:
    *snip*

    Yeah, but that's extremelly coupled not only to the event itself, but to its name. The approach I'm looking for is a completelly generic way to convert any delegate type into an Observable - not only a specific event or an event with a specific name.

    . . .

  • User profile image
    Ion Todirel

    Santa's Envoy said:
    JoshRoss said:
    *snip*

    Yeah, but that's extremelly coupled not only to the event itself, but to its name. The approach I'm looking for is a completelly generic way to convert any delegate type into an Observable - not only a specific event or an event with a specific name.

    There isn't  a way of doing it. Or it would be coupled.

  • User profile image
    TommyCarlier

    You mean something like this?

    var mouseDown = Observable.FromEvent<MouseButtonEventArgs>(control, "MouseDown");

    See Wes Dyer's Brief Introduction to the Reactive Extensions for .NET.

  • User profile image
    Santa's Envoy

    Ion Todirel said:
    Santa's Envoy said:
    *snip*

    There isn't  a way of doing it. Or it would be coupled.

    Would you elaborate on that? The whole question is the decoupling which we don't have. We have to provide boilerplate code for every event/delegate we transform into observables, or worse: we might have to reflect on the event's name, which further couples the conversion.

  • User profile image
    Santa's Envoy

    TommyCarlier said:

    You mean something like this?

    var mouseDown = Observable.FromEvent<MouseButtonEventArgs>(control, "MouseDown");

    See Wes Dyer's Brief Introduction to the Reactive Extensions for .NET.

    Nope. That is the current solution we have today. What I'd like to know is why don't we have something similar to the code I posted - a clean generic function callable in any EventHandler type to get an Observable.

     

    Heck, I could dig a whole lot deeper on features missing for Observables, but I'm particularly interested in this specific one.

  • User profile image
    Richard.Hein

    Santa's Envoy said:
    TommyCarlier said:
    *snip*

    Nope. That is the current solution we have today. What I'd like to know is why don't we have something similar to the code I posted - a clean generic function callable in any EventHandler type to get an Observable.

     

    Heck, I could dig a whole lot deeper on features missing for Observables, but I'm particularly interested in this specific one.

    There is another overload that doesn't take a string - it's all strongly typed.  However, what you really want is first class events, which neither C# nor VB.NET support.  F# does, however, have first class events.  You cannot create an extension method for an event, because events are not objects or structs, they do not have methods to invoke in the same way as first class objects.

     

    There are however, code generators that will do all that for you, already.

  • User profile image
    Santa's Envoy

    Richard.Hein said:
    Santa's Envoy said:
    *snip*

    There is another overload that doesn't take a string - it's all strongly typed.  However, what you really want is first class events, which neither C# nor VB.NET support.  F# does, however, have first class events.  You cannot create an extension method for an event, because events are not objects or structs, they do not have methods to invoke in the same way as first class objects.

     

    There are however, code generators that will do all that for you, already.

    Hmm. Interesting.  Would you mind elaborating on that? That is exactly what I want to know.

  • User profile image
    Ion Todirel

    Santa's Envoy said:
    Richard.Hein said:
    *snip*

    Hmm. Interesting.  Would you mind elaborating on that? That is exactly what I want to know.

    Elaborate on what? You can't "auto-detect" what a user does, intended to do, or the world is like. You could probably mark a collection / property or events with special attributes and tight on them in the extension method, but it's not universal, and it would be pretty hard to make it all thread safe without adjusting the original implementation. If you have a sample which works well in all possible cases, is thread safe and looks nice, I may suggest it to the CLR team for future considerations.

  • User profile image
    Richard.Hein

    Santa's Envoy said:
    Richard.Hein said:
    *snip*

    Hmm. Interesting.  Would you mind elaborating on that? That is exactly what I want to know.

    Here's one such tool "reactive framework extensions generator":  http://clarius.codeplex.com/wikipage?title=Reactive

     

     

  • User profile image
    Santa's Envoy

    Ion Todirel said:
    Santa's Envoy said:
    *snip*

    Elaborate on what? You can't "auto-detect" what a user does, intended to do, or the world is like. You could probably mark a collection / property or events with special attributes and tight on them in the extension method, but it's not universal, and it would be pretty hard to make it all thread safe without adjusting the original implementation. If you have a sample which works well in all possible cases, is thread safe and looks nice, I may suggest it to the CLR team for future considerations.

    I asked you to elaborate on your post because I believe you did not get the point of the thread. What I'm asking, in a nutshel, is why I can't create a generic method (as in an extension method) which could convert any events (delegate types) into Observables. According to Richard, it lies on the mechanics of delegate types. Although the specifics are yet quite unclear, I believe that is a logical path of reasoning.

     

    Cheers.

  • User profile image
    Santa's Envoy

    Richard.Hein said:
    Santa's Envoy said:
    *snip*

    There is another overload that doesn't take a string - it's all strongly typed.  However, what you really want is first class events, which neither C# nor VB.NET support.  F# does, however, have first class events.  You cannot create an extension method for an event, because events are not objects or structs, they do not have methods to invoke in the same way as first class objects.

     

    There are however, code generators that will do all that for you, already.

    Could you point me to some reference material over the mechanics of delegate types? Most material I dug up don't mention the specifics of delegates, nor a comparison of first class events and regular delegates. Thanks.

  • User profile image
    Maddus Mattus

    Santa's Envoy said:
    Richard.Hein said:
    *snip*

    Could you point me to some reference material over the mechanics of delegate types? Most material I dug up don't mention the specifics of delegates, nor a comparison of first class events and regular delegates. Thanks.

    You cant make a extention method on a delegate, because a delegate is not an event.

     

    The event keyword turns the delegate into an event.

     

    Seeing as you cant write extension methods for keywords, they have made the workaround you've seen in this thread.

     

    Like Erik and his team says; This is by no means a replacement for events, it just makes it easyer to work with them.

  • User profile image
    Santa's Envoy

    Maddus Mattus said:
    Santa's Envoy said:
    *snip*

    You cant make a extention method on a delegate, because a delegate is not an event.

     

    The event keyword turns the delegate into an event.

     

    Seeing as you cant write extension methods for keywords, they have made the workaround you've seen in this thread.

     

    Like Erik and his team says; This is by no means a replacement for events, it just makes it easyer to work with them.

    Well, you'll have to tell me, then, exactly how the event keyword modifies the delegate-type instance. As far as I'm concerned, it only encapsulates and limits access to the underlying object. Either way, it has already been shown[*] that it does not matter whether the variable has been declared with the event modifier; it will not work either way (* Code in the first post).

     

    I did not understand your point about writing extension methods for keywords. The event keyword simply tells the compiler to wrap that instance of the delegate type (in some way I do not pretend I know) and limit access to it. The underlying type of the object is of the type you declared the event with. Thus, it is that type I'm going to try and extend. If you do understand more about the subject, I'd love to hear it. Again, it does not appear to be the case that the event keyword is responsible for this, though.

     

    And for the last point: Yes, it pretty much is. I'm sure Erik wouldn't say it - or maybe he meant that the .NET Framework won't be rewritten to use Observables, or problably a combination of both - but it is obvious that that's exactly the idea (and understandably so).  Though, as we all know, "replacing events" is a fairly simplistic view of Observable's concept.

     

    Cheers.

  • User profile image
    Maddus Mattus

    Santa's Envoy said:
    Maddus Mattus said:
    *snip*

    Well, you'll have to tell me, then, exactly how the event keyword modifies the delegate-type instance. As far as I'm concerned, it only encapsulates and limits access to the underlying object. Either way, it has already been shown[*] that it does not matter whether the variable has been declared with the event modifier; it will not work either way (* Code in the first post).

     

    I did not understand your point about writing extension methods for keywords. The event keyword simply tells the compiler to wrap that instance of the delegate type (in some way I do not pretend I know) and limit access to it. The underlying type of the object is of the type you declared the event with. Thus, it is that type I'm going to try and extend. If you do understand more about the subject, I'd love to hear it. Again, it does not appear to be the case that the event keyword is responsible for this, though.

     

    And for the last point: Yes, it pretty much is. I'm sure Erik wouldn't say it - or maybe he meant that the .NET Framework won't be rewritten to use Observables, or problably a combination of both - but it is obvious that that's exactly the idea (and understandably so).  Though, as we all know, "replacing events" is a fairly simplistic view of Observable's concept.

     

    Cheers.

    I understand your logic, but I dont think it will work.

     

    For instance, you dont specify wich object to observe on. A event in itself is meaningless. It's the context of the event that provides the added value. A mouse move event is meaningless without the mouse object. So you cannot observe a mouse move event without an instance of a mouse.

     

    You are trying to create a static extension method for events. The static has no reference to the instance, so what is it listening to?

  • User profile image
    Santa's Envoy

    Maddus Mattus said:
    Santa's Envoy said:
    *snip*

    I understand your logic, but I dont think it will work.

     

    For instance, you dont specify wich object to observe on. A event in itself is meaningless. It's the context of the event that provides the added value. A mouse move event is meaningless without the mouse object. So you cannot observe a mouse move event without an instance of a mouse.

     

    You are trying to create a static extension method for events. The static has no reference to the instance, so what is it listening to?

    What the extension method is supposed to do is simply return an Observable representation of the event, so you can access a richer set of functionallity. Contextual information is not lost because you can only obtain said Observable by calling the method on the event. The model we have available today (Observable.FromEvent<>) is much less intuitive (and much more verbose) than the extension method solution. Though the semantics aren't really my matter of interest. What I'm interested to know is why it can't be done. If it could, I'm pretty sure it would've.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.