- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Alternative to Annotations
Sun, 2011-04-24, 10:49
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
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
Sun, 2011-04-24, 20:37
#2
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:
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
On 24 April 2011 10:49, Antonio Jr. Mattos <amattos.mail@gmail.com> wrote:
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