Skip to content

Instantly share code, notes, and snippets.

@Jasper-M
Last active December 9, 2016 15:48
Show Gist options
  • Save Jasper-M/0103673e3298fe5705d99e711f97b265 to your computer and use it in GitHub Desktop.
Save Jasper-M/0103673e3298fe5705d99e711f97b265 to your computer and use it in GitHub Desktop.
dotty implicit function types
object ImplicitFunShowTest {
import Showing._
def showList[T](list: List[T]) = list.map(t => show(t): String)
// no implicit argument of type Showing.Show[T] found for parameter x$0 of method apply in trait ImplicitFunction1
def main(args: Array[String]): Unit = {
println( showList(List("a","b","c")) )
println( showList(List(1,2,3)) )
}
}
object ImplicitFunShowTest {
import Showing._
def showList[T](list: List[T]): Showable[T, List[String]] = list.map(t => show(t))
// found: implicit Showing.Show[T] =>
// scala.collection.immutable.List[Showing.Showable[T, String] | String]
// required: Showing.Showable[T, scala.collection.immutable.List[String]]
def main(args: Array[String]): Unit = {
println( showList(List("a","b","c")) )
println( showList(List(1,2,3)) )
}
}
object ImplicitFunShowTest {
import Showing._
def showList[T](list: List[T]): Showable[T, List[String]] = list.map(t => thisShow.show(t))
def main(args: Array[String]): Unit = {
println( showList(List("a","b","c")) )
println( showList(List(1,2,3)) )
}
}
object ImplicitFunShowTest {
import Showing._
def showList[T](list: List[T]): Showable[T, List[String]] = list.map(t => show(t): String)
def main(args: Array[String]): Unit = {
println( showList(List("a","b","c")) )
println( showList(List(1,2,3)) )
}
}
object ImplicitFunShowTest {
import Showing._
def showList[T](list: List[T]) = { implicit _: Show[T] =>
list.map(t => show(t))
}
def main(args: Array[String]): Unit = {
println( showList(List("a","b","c")) )
println( showList(List(1,2,3)) )
}
}
object Showing {
trait Show[T] { def show(t: T): String }
object Show {
implicit val showString: Show[String] = new Show[String] {
def show(t: String) = t
}
implicit val showInt: Show[Int] = new Show[Int] {
def show(t: Int) = t.toString
}
}
type Showable[T,R] = implicit Show[T] => R
def thisShow[T]: Showable[T,Show[T]] = implicitly[Show[T]]
def show[T](t: T): Showable[T,String] = thisShow.show(t)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment