Unwrapping the wrapper
When we started development on the Scala DSL for Camel, we were using implicit conversion to convert a Java DSL type into a Scala DSL type like this:
However, when we try to create a nice-looking DSL this way, we run into problems with the explicits-first rule for implicit conversion in Scala sometimes: methods on the original Java DSL type would take precedence over the 'new' Scala DSL methods.
We actually wanted the behavior to be the other way around: prefer the Scala DSL methods and only fail back to the Java DSL when necessary. Just to whet your appetite for Scala, all it takes to implement this is a simple trait...
... and one equally simple implicit conversion method.
implicit def enrichChoice(choice: ChoiceType) = new RichChoiceType(choice)
However, when we try to create a nice-looking DSL this way, we run into problems with the explicits-first rule for implicit conversion in Scala sometimes: methods on the original Java DSL type would take precedence over the 'new' Scala DSL methods.
We actually wanted the behavior to be the other way around: prefer the Scala DSL methods and only fail back to the Java DSL when necessary. Just to whet your appetite for Scala, all it takes to implement this is a simple trait...
... implemented on our Scala DSL types ...
trait Wrapper[T] {
val unwrap : T
}
class RichChoiceType(val unwrap: ChoiceType) with Wrapper[ChoiceType]
... and one equally simple implicit conversion method.
implicit def unwrap[T](wrapper: Wrapper[T]) = wrapper.unwrap
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home