Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I couldn't claim that this is a canonical property change framework in Scala, but I've used a class like this before:</p> <pre><code>abstract class Notifier[T,U](t0: T) { import java.util.concurrent.atomic.AtomicReference import scala.actors.OutputChannel type OCUT = OutputChannel[(U,AtomicReference[T])] val data = new AtomicReference[T](t0) def id: U protected var callbacks = Nil:List[T =&gt; Unit] protected var listeners = Nil:List[OCUT] def apply() = data.get def update(t: T) { val told = data.getAndSet(t) if (t != told) { callbacks.foreach(_(t)) listeners.foreach(_ ! (id,data)) } } def attend(f: T=&gt;Unit) { callbacks ::= f } def attend(oc: OCUT) { listeners ::= oc } def ignore(f: T=&gt;Unit) { callbacks = callbacks.filter(_ != f) } def ignore(oc: OCUT) { listeners = listeners.filter(_ != oc) } } </code></pre> <p>The motivation for creating this class was that I wanted a flexible thread-safe way to react to changes, which this provides (as it delivers both callbacks and can push messages to actors).</p> <p>It seems to me--unless I'm misunderstanding exactly what you want because I haven't had occasion to learn the WPF/Silverlight stuff--that this can implement everything you want and more.</p> <p>For example,</p> <pre><code>class IDrawable extends SomethingWithOnPropertyChanged { val drawOrder = new Notifier[Int,Symbol](0) { def id = 'DrawOrder } val visible = new Notifier[Boolean,Symbol](false) { def id = 'Visible } drawOrder.attend((i:Int) =&gt; OnPropertyChanged(drawOrder.id)) def mutate { if (visible()) drawOrder() += 1 } } </code></pre> <p>should be roughly equivalent to what you want. (Again, I'm not sure how flexible you want this to be; you could create a set of symbol -> notifier mappings that you would look up with an apply method so the target would have an easier time of doing something when it gets the DrawOrder symbol.)</p> <p>The only significant difference from your usage is that the Notifier uses its apply/update methods to save boilerplate; you don't have to write def x and def x_= methods every time, but you do have to use () for access.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload