Let's say I have several functions:
func1 : A => B
func2: B => C
func3: C => D
I would like to orchestrate now functions when needed in a generic fashion.
let's say if i need a conversion from A
to B
I'd call func1
.
But when I need a conversion from A
to D
I would like to have a composition of those functions. Is such thing possible in a dynamic notion?
From the Scala documentation for language features, explaining why implicit conversions have to be explicitly enabled in 2.10:
User-defined implicit conversions are almost always a bad idea, and making them transitive would be much, much worse.
You can, however, use type classes to get a similar effect in a much safer, more controlled way. For example, suppose we have the following:
Now we can write:
And finally:
We've never explicitly defined a converter from integers to strings, but the compiler is able to use our
composeMyConverters
method to construct one when it's needed.Like implicit conversions, this approach can also be abused, but it's much easier to keep tabs on what converters are in scope, where they're being applied, etc.