This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Alternative to Annotations

2 replies
Antonio Jr. Mattos
Joined: 2011-04-24,
User offline. Last seen 42 weeks 4 days ago.
Hi every one, I am new to Scala programming so I'm having a hard time to solve the follow problem:    - I have an application that takes a user Java class where methods are annotated with additional information such as:     order of execution, timeout, range of execution. This information is used to create a schedule to execute  the user's    class methods across multiples nodes in the cluster. The method below is the first method executed on nodes 1 to 10.       @Step(order = 1, timeout = 1000000, range = "1-10")   public void doSomething(){}
   Because the code base (in Java) is getting more complicated to manage and I need a more flexible and powerful    solution a looked to another solution and found Scala. The only problem I have found is to enable a simple way   to let users define their classes, like the code above. I wrote some code using case classes with objects as    parameters along with the "metadata"
   case class Step(order: Int, timeout: Long, range: String, funcObj: FuncObj)
   This enables funcObj to be sent to the nodes. But this is harder to the user that now needs code FuncObj and then write a    case class to that. I thought I could use something like
   doSomething("STEP", order, timeout, range) {      // code for FuncObj ..
      // code to create and return a case class Step(..., funcObj)    }
   But this is awkward (and besides I don't know how to code this - put FuncObj inside Step() ) . If any, I'd like suggestions. Thanks. 
      
--
Antonio J Mattos

Mestrado Informática UFPR
Celular:   (42) 9913-5684
Skype:    antonioj.mattos
Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Alternative to Annotations


On 24 April 2011 10:49, Antonio Jr. Mattos <amattos.mail@gmail.com> wrote:
Hi every one, I am new to Scala programming so I'm having a hard time to solve the follow problem:    - I have an application that takes a user Java class where methods are annotated with additional information such as:     order of execution, timeout, range of execution. This information is used to create a schedule to execute  the user's    class methods across multiples nodes in the cluster. The method below is the first method executed on nodes 1 to 10.       @Step(order = 1, timeout = 1000000, range = "1-10")   public void doSomething(){}
   Because the code base (in Java) is getting more complicated to manage and I need a more flexible and powerful    solution a looked to another solution and found Scala. The only problem I have found is to enable a simple way   to let users define their classes, like the code above. I wrote some code using case classes with objects as    parameters along with the "metadata"
   case class Step(order: Int, timeout: Long, range: String, funcObj: FuncObj)
   This enables funcObj to be sent to the nodes. But this is harder to the user that now needs code FuncObj and then write a    case class to that. I thought I could use something like
   doSomething("STEP", order, timeout, range) {      // code for FuncObj ..
      // code to create and return a case class Step(..., funcObj)    }
   But this is awkward (and besides I don't know how to code this - put FuncObj inside Step() ) . If any, I'd like suggestions. Thanks. 
      
--
Antonio J Mattos

Mestrado Informática UFPR
Celular:   (42) 9913-5684
Skype:    antonioj.mattos


define it as:
   class Step(val order: Int, val timeout: Int, val range: Range, val func: => Unit)
   object Step {      def apply(order: Int, timeout: Int, range: Range)(func: => Unit) =        new Step(order, timeout, range, func)    }
This uses apply on a companion object to act as a factory (or smart constructor, if you prefer that term), and a by-name param to encapsulate the step logic.
The second (by-name) parameter block can then be supplied in braces, as per your example:
    val myStep = Step(7, 15, 1 to 10) {      //code      //more code     }
--
Kevin Wright

gtalk / msn : kev.lee.wright@gmail.comkev.lee.wright@gmail.commail: kevin.wright@scalatechnology.com
vibe / skype: kev.lee.wrightquora: http://www.quora.com/Kevin-Wright
twitter: @thecoda

"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra
vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: Alternative to Annotations
I may be wrong, but the solution seems to be so specific for Java which does not have citizenship rights for functions. If it did, you would just have a decorator for your function class, and this decorator would not need an out-of-the-language notion of annotations.

Thanks,
-Vlad


On Sun, Apr 24, 2011 at 2:49 AM, Antonio Jr. Mattos <amattos.mail@gmail.com> wrote:
Hi every one, I am new to Scala programming so I'm having a hard time to solve the follow problem:    - I have an application that takes a user Java class where methods are annotated with additional information such as:     order of execution, timeout, range of execution. This information is used to create a schedule to execute  the user's    class methods across multiples nodes in the cluster. The method below is the first method executed on nodes 1 to 10.       @Step(order = 1, timeout = 1000000, range = "1-10")   public void doSomething(){}
   Because the code base (in Java) is getting more complicated to manage and I need a more flexible and powerful    solution a looked to another solution and found Scala. The only problem I have found is to enable a simple way   to let users define their classes, like the code above. I wrote some code using case classes with objects as    parameters along with the "metadata"
   case class Step(order: Int, timeout: Long, range: String, funcObj: FuncObj)
   This enables funcObj to be sent to the nodes. But this is harder to the user that now needs code FuncObj and then write a    case class to that. I thought I could use something like
   doSomething("STEP", order, timeout, range) {      // code for FuncObj ..
      // code to create and return a case class Step(..., funcObj)    }
   But this is awkward (and besides I don't know how to code this - put FuncObj inside Step() ) . If any, I'd like suggestions. Thanks. 
      
--
Antonio J Mattos

Mestrado Informática UFPR
Celular:   (42) 9913-5684
Skype:    antonioj.mattos

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland