Created
March 27, 2019 09:00
-
-
Save fsarradin/91e834417069d43f3cc1cc07c1624670 to your computer and use it in GitHub Desktop.
Typeclass in Dotty 0.13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def (x: Truc) op(y: B): C
est une méthode d'extension et permet d'ajouter l'opérationop
aux instances deTruc
.implied X for Bibule { ... }
permet de déclarer une instance implicite pour le type Bidule. C'est l'équivalent deimplicit object X extends Bidule { ... }