简体   繁体   中英

About Scala fields and property change events

As a followup to this 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM