- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
aspectJ experiment: observing property changes
Wed, 2009-04-01, 21:00
I tried an experiment, using aspectJ to insert property change detection into scala instances. It works fine, at least on my small test case. You can see what I did here on git.
Lee
It's pretty neat that idiomatic scala properties can be observed for changes
with almost no changes to the scala source. (It'd be great if this could be done
in scala without resorting to aspectJ...).
The scala class needs only to add the marker trait Observable.
class Model extends AnyRef with Observable {
var prop:String = _
}
Modifications to the instance will then run the aspect, notifying the observer.
val model = new Model()
m.prop = "whee"
Run that and the test observer sees the change and prints:
change observed> example.Model@6abf2d5e=>> prop:whee
I'm getting more tempted to use aspectJ because it enables such a simple and idiomatic
approach.
Lee
Wed, 2009-04-01, 22:07
#2
Re: aspectJ experiment: observing property changes
Hi dean -- BTW, I looked at your blog post to try this experiment, thanks.
I agree it would be better to do this w/o aspectJ. But there are some disadvantages -- mainly that overriding each property manually creates an unfortunate amount of boilerplate to write and maintain. You have to create an override for every property, always change two places when you add/remove/change a property, and potentially edit every override if your observation api changes (e.g. to report the original state of the property).
Overriding manually might still be the best approach on balance, but you see the temptation to mess around with aspects. The tooling is awkward, but the net resulting api is clean.
Lee
On Wed, Apr 1, 2009 at 1:32 PM, Dean Wampler <deanwampler@gmail.com> wrote:
I agree it would be better to do this w/o aspectJ. But there are some disadvantages -- mainly that overriding each property manually creates an unfortunate amount of boilerplate to write and maintain. You have to create an override for every property, always change two places when you add/remove/change a property, and potentially edit every override if your observation api changes (e.g. to report the original state of the property).
Overriding manually might still be the best approach on balance, but you see the temptation to mess around with aspects. The tooling is awkward, but the net resulting api is clean.
Lee
On Wed, Apr 1, 2009 at 1:32 PM, Dean Wampler <deanwampler@gmail.com> wrote:
While AspectJ certainly works for this, you can also achieve the same effect with traits. Something like
trait ObservedPropWrite extends Model { override def prop_=(x: String) { super.prop_=(x) log("change observed> " + this + "=>> prop:" +prop) }}
val model = new Model with ObservedPropWrite
Where AspectJ is unique is when this kind of modification is a pervasive behavior change and there is no common trait, etc. that you can use as the single point to intercept or you don't have control over instance instantiation. Then the "pointcut language" and "advice" mechanisms are really handy.
dean
On Wed, Apr 1, 2009 at 2:59 PM, Lee Mighdoll <lee@underneath.ca> wrote:
I tried an experiment, using aspectJ to insert property change detection into scala instances. It works fine, at least on my small test case. You can see what I did here on git.
It's pretty neat that idiomatic scala properties can be observed for changes
with almost no changes to the scala source. (It'd be great if this could be done
in scala without resorting to aspectJ...).
The scala class needs only to add the marker trait Observable.
class Model extends AnyRef with Observable {
var prop:String = _
}
Modifications to the instance will then run the aspect, notifying the observer.
val model = new Model()
m.prop = "whee"
Run that and the test observer sees the change and prints:
change observed> example.Model@6abf2d5e=>> prop:whee
I'm getting more tempted to use aspectJ because it enables such a simple and idiomatic
approach.
Lee
--
Dean Wampler
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
- http://groups.google.com/group/chicagoscala
- http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
Wed, 2009-04-01, 22:27
#3
Re: aspectJ experiment: observing property changes
All true.
I'd like to figure out ways to get the decoupling that aspects provide, but still use traits, in some way. Java annotations provide a much better way to define "pointcuts", since method and field names can change on you, but annotations are generally more stable.
Perhaps an object factor that mixes in traits based on the annotations on the methods and fields?
dean
On Wed, Apr 1, 2009 at 4:01 PM, Lee Mighdoll <lee@underneath.ca> wrote:
--
Dean Wampler
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
- http://groups.google.com/group/chicagoscala
- http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
I'd like to figure out ways to get the decoupling that aspects provide, but still use traits, in some way. Java annotations provide a much better way to define "pointcuts", since method and field names can change on you, but annotations are generally more stable.
Perhaps an object factor that mixes in traits based on the annotations on the methods and fields?
dean
On Wed, Apr 1, 2009 at 4:01 PM, Lee Mighdoll <lee@underneath.ca> wrote:
Hi dean -- BTW, I looked at your blog post to try this experiment, thanks.
I agree it would be better to do this w/o aspectJ. But there are some disadvantages -- mainly that overriding each property manually creates an unfortunate amount of boilerplate to write and maintain. You have to create an override for every property, always change two places when you add/remove/change a property, and potentially edit every override if your observation api changes (e.g. to report the original state of the property).
Overriding manually might still be the best approach on balance, but you see the temptation to mess around with aspects. The tooling is awkward, but the net resulting api is clean.
Lee
On Wed, Apr 1, 2009 at 1:32 PM, Dean Wampler <deanwampler@gmail.com> wrote:While AspectJ certainly works for this, you can also achieve the same effect with traits. Something like
trait ObservedPropWrite extends Model { override def prop_=(x: String) { super.prop_=(x) log("change observed> " + this + "=>> prop:" +prop) }}
val model = new Model with ObservedPropWrite
Where AspectJ is unique is when this kind of modification is a pervasive behavior change and there is no common trait, etc. that you can use as the single point to intercept or you don't have control over instance instantiation. Then the "pointcut language" and "advice" mechanisms are really handy.
dean
On Wed, Apr 1, 2009 at 2:59 PM, Lee Mighdoll <lee@underneath.ca> wrote:
I tried an experiment, using aspectJ to insert property change detection into scala instances. It works fine, at least on my small test case. You can see what I did here on git.
It's pretty neat that idiomatic scala properties can be observed for changes
with almost no changes to the scala source. (It'd be great if this could be done
in scala without resorting to aspectJ...).
The scala class needs only to add the marker trait Observable.
class Model extends AnyRef with Observable {
var prop:String = _
}
Modifications to the instance will then run the aspect, notifying the observer.
val model = new Model()
m.prop = "whee"
Run that and the test observer sees the change and prints:
change observed> example.Model@6abf2d5e=>> prop:whee
I'm getting more tempted to use aspectJ because it enables such a simple and idiomatic
approach.
Lee
--
Dean Wampler
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
- http://groups.google.com/group/chicagoscala
- http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
--
Dean Wampler
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
- http://groups.google.com/group/chicagoscala
- http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
Thu, 2009-04-02, 08:37
#4
Re: aspectJ experiment: observing property changes
Lee Mighdoll wrote:
> I tried an experiment, using aspectJ to insert property change detection
> into scala instances. It works fine, at least on my small test case.
> You can see what I did here on git
> .
>
> It's pretty neat that idiomatic scala properties can be observed for changes
> with almost no changes to the scala source. (It'd be great if this could be done
> in scala without resorting to aspectJ...).
trait Disposable {
def dispose : Unit
}
trait SignalConnection[T] extends Disposable {
def signal : Signal[T]
}
trait Signal[T] {
def observe(proc : T => Unit) : SignalConnection[T]
}
trait Var[T] {
def apply(v : T) : Unit
def apply : T
}
trait ObservableVar[T] extends Var[T] {
def changedSignal : Signal[ObservableVar[T]]
}
Implementation is left as an exercise.
/Jesper Nordenberg
trait ObservedPropWrite extends Model { override def prop_=(x: String) { super.prop_=(x) log("change observed> " + this + "=>> prop:" +prop) }}
val model = new Model with ObservedPropWrite
Where AspectJ is unique is when this kind of modification is a pervasive behavior change and there is no common trait, etc. that you can use as the single point to intercept or you don't have control over instance instantiation. Then the "pointcut language" and "advice" mechanisms are really handy.
dean
On Wed, Apr 1, 2009 at 2:59 PM, Lee Mighdoll <lee@underneath.ca> wrote:
--
Dean Wampler
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
- http://groups.google.com/group/chicagoscala
- http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org