About Scala fields and property change events
As a followup to t开发者_如何转开发his question about Scala's @BeanProperty generating change events: What would it take to fully implement the behavior that annotating a var field with some custom annotation (@Property, for instance) would generate the code needed to fire property change events? The only way to do it is to write a compiler plugin, right?
More generally: is there a standard way (like in Java) to process annotations in the compiler in Scala?
It may be possible to do this with a proxy, just as you would in Java, possibly involving cglib or similar. A compiler plugin could also do this (as you rightly state), but might be a bit overkill if you're writing anything less than a general-purpose library!
A far better solution would be to manually write the getter and setter methods by hand so that they emit these events, if you're concerned about keeping code clean then these could always be moved into a trait.
For a slightly different approach to the problem, Naftoli Gugenhem has a "reactive" library on GitHub to help with Functional Reactive Programming, arguably a better paradigm than the event-driven model of observable properties.
The ObservableBuffer class is a good place to start looking.
精彩评论