2

I have an application where some var values ought to be published to a message queue on change. That is, if a var's setter is invoked I want this to be noticed somehow and after setting the new value I want it to be published to the MQ.

I did something like this some time ago with Perl/Moose by setting an after-modifier (doing the publishing) on methods with a certain method attribute. That solution was very elegant and required no syntactical overhead besides the additional method attribute.

What would be a good solution using Scala's (2.10) capabilities without using clumsy OO patterns?

Update: What I would like to achieve is that the code looks something like this:

@Publishable var someProperty = 42

or

domainSpecificLanguageMagic someProperty = 42

One of the challenges is that these properties might be reflectively set, so a setter-method with a different name is (probably?) not an option.

tobi
  • 81
  • 6

2 Answers2

0

I guess the way to go is scala virtualized: it has some built in primitives that allow you to change language semantic including assignment one. It isn't stock scala , but it is quite officially supported and new release usually comes no too late after the ordinary one (in a matter of weeks). (I'm confused it with another scala framework -- LMS, AFAIS it isn't supported quite good ATM, but still should solve your problem)

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
0

You can make the property private (and give it a variant name) and define its accessor and mutator methods:

class C1(...) {
  private var iProp: Int = 0

  def prop: Int = iProp

  /* Put additional logic associated with mutation here: */
  def prop_=(newProp: Int): Unit = iProp = newProp
}
Randall Schulz
  • 26,420
  • 4
  • 61
  • 81