Reputation: 453
I have a user defined alias for a higher kinded type in scala:
type FutureOfLastError = Future[LastError]
I also have a value of that type:
val myFuture: FutureOfLastError = ...
To write readable Code I would like to define (for example) a method like
def mapToString = { ... }
which maps an instance of FutureOfLastError to a Future[String] and call it like
myFuture mapToString
How can I do this? Is it even possible? I already tried encapsulation in a case class, all kinds of implicit conversions that came to my mind.
My favourite choice would be a companion object on the type-alias, but I couldn't get it to work because I cannot call the methods of trait Future (like map, etc.) from the companion object.
Upvotes: 1
Views: 129
Reputation: 32739
You don't need to actually extend the class, you can just use an implicit class (which is just syntactic sugar for the "enrich my library" pattern). Something as simple as this should do it:
implicit class MyFutureOps( future: FutureOfLastError ) {
def mapToString = future.map(_.toString)
}
Upvotes: 1