observable scala objects

View: New views
1 Messages — Rating Filter:   Alert me  

observable scala objects

by mighdoll :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How 'bout a compiler plugin to make plain old mutable scala objects observable?    I think libraries in Ruby, Python, or Javascript can use observation without requiring much compromise by their users...  I woke up this morning wondering what changes to scala would be required to enable no-compromise observation.

Imagine the user of the library has a simple object that they want the library to watch:

    class Model(var name:String)


Here's one idea:
       
    class Model(var name:String) extends Observable[Model]



And then the library could do something like this.  (I'm sure there's a better syntax.)

    trait Observable[T] { 
      override def  *_=(arg:_) = {   // * to override all matching methods
                super._=(arg)
        notify()
      }
    }




While it's possible to make a library that uses observation in scala today, as far as I know you have to force library users to give up either compatibility or maintainability.  

We could require that users create a trait with some manual overrides for notification.  But this creates significant maintenance work for the user of the library who has to maintain the notification calls.

    trait ObservableModel {
      def override name_=(value:String) = { super._=(value);  notify() }
    }


Alternately, we can use a delegating object for each property.  But then the object is incompatible with other libraries, JPA, etc.  And the library asks maintenance effort from it's users to learn and commit to modeling objects using the delegation scheme. 
     
      class ObservableModel(var name:ObservableVar[String])