How can I get rid of parentheses in a scala DSL expression?

I would like to get rid of the brackets / brackets in the following expression in DSL:

substitute ("hello {0}" using "world")

The rest of the code is as follows:

 class Rule(format: String) { def using(arggs: String*): Rule = { /* save the args */ return this } def execute() = { /* substitute params */ } } def substitute(rule: Rule) = rule.execute() implicit def makeRule(format: String) = new Rule(format) 

I tried the apply () method, but I don't think I can do it. Is there scala magic that I could use?

+4
source share
2 answers

Scala has its own equivalent of an iamb / iambic meter (if you want) if you want to omit it . and ( ... ) :

target (skipped . ) method (skipped ( ) singleArgument (skipped ) )

The singleArgument parameter is optional.

Every pointless, indifferent DSL-ish thing must fit this pattern. This is why Scala internal DSL drives bend so often.

+3
source

An inaccurate solution for your case (@RandallSchulz is absolutely right), but you can use a two-word approach:

 class RuleHelper(val txt: String) { def using(arggs: String*): RuleHelper = { /* save the args */ return this } def execute() = { /* substitute params */ } } object substitute { def in(txt: String) = new RuleHelper(txt) } substitute in "hello {0}" using "world" 

It is not as beautiful as we would like, but I think it reduces random complexity.

+1
source

All Articles