Skip to content

Instantly share code, notes, and snippets.

@fsarradin
Created March 27, 2019 09:00
Show Gist options
  • Save fsarradin/91e834417069d43f3cc1cc07c1624670 to your computer and use it in GitHub Desktop.
Save fsarradin/91e834417069d43f3cc1cc07c1624670 to your computer and use it in GitHub Desktop.
Typeclass in Dotty 0.13
trait Functor[F[_]] {
def (fa: F[A]) map[A, B](f: A => B): F[B]
}
case class IO[+A](run: () => A)
object IO {
def pure[A](a: => A): IO[A] = IO(() => a)
implied IOFunctor for Functor[IO] {
def (fa: IO[A]) map[A, B](f: A => B): IO[B] =
pure(f(fa.run()))
}
}
object TypeclassMain {
import IO.IOFunctor
def main(args: Array[String]): Unit = {
val io = IO.pure(10)
println(s"io: ${io.run()}")
val result = io.map(_ * 2)
println(s"result: ${result.run()}")
}
}
@fsarradin
Copy link
Author

def (x: Truc) op(y: B): C est une méthode d'extension et permet d'ajouter l'opération op aux instances de Truc.

implied X for Bibule { ... } permet de déclarer une instance implicite pour le type Bidule. C'est l'équivalent de implicit object X extends Bidule { ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment