Typeclass
-
Mechanism
- TypeClass is parameterized
trait - TypeClass Instance is a concrete implementation of TypeClass for a particular Type tagged with
implicit - Type Class Interface is functionality/generic method(s) that accepts Type Class instance
- Interface Object define Objects with methods that accept a type class instance implicitly
- Interface Syntax defines extension method by providing
implicitclass definition
- TypeClass is parameterized
-
Scala checks for an
implicitinstance of typeclassTC[T]in companion objects of both,TCandT -
Best practice: Use context bound syntax and define a companion object to instantiate typeclass
-
implicitdefinitions cannot be at package level, but must be placed in anobjector atrait -
Avoid structural types because they use reflection at run-time.
-
defcan be overridden withvalbut not the other way around -
implicit classare syntactic sugar fordef+class -
Syntactic Sugar
// following two are equivalentdef combineAll[A](list: List[A])(implicit A: Monoid[A]): A = ???def combineAll[A : Monoid](list: List[A]): A = ??? // context-bound syntax sugar// context bound syntax sugar requires use of implicitlydef implicitly[A](implicit ev: A): A = ev // this is already defined in std libdef combineAll[A : Monoid](list: List[A]): A =list.foldRight(implicitly[Monoid[A]].empty)(implicitly[Monoid[A]].combine)// Instead of calling implicitly everywhere, most cats typeclasses defineobject Monoid {def apply[A : Monoid]: Monoid[A] = implicitly[Monoid[A]]}def combineAll[A : Monoid](list: List[A]): A =list.foldRight(Monoid[A].empty)(Monoid[A].combine)
using and given
Section titled “using and given”-
there can be multiple parameters list with
using, and can be mixed with regular parameter listsusingparameters don’t need to be named, you can usesummon[Ordering[Int]]
-
givencan be instances or an alias. Either way it’s initialized only the first time it is used// given instanceobject Ordering:given Int: Ordering[Int] withdef compare(x: Int, y: Int): Int = ???// given aliasobject IntOrdering extends Ordering[Int]:def compare(x: Int, y: Int): Int = ???given intOrdering: Ordering[int] = IntOrdering -
giveninstances or aliases don’t need to be namedobject Ordering:given Ordering[Int] withdef compare(x: Int, y: Int): Int = ???object IntOrdering extends Ordering[Int]:def compare(x: Int, y: Int): Int = ???given Ordering[int] = IntOrdering -
sub-class
giveninstances are at higher priority than parentgiveninstances// priorityclass General()class Specific() extends General()given general: General = General()given specific: Specific = Specific()summon[General] // picks given specific -
conditional
givendefinition has ausingclause: agivendefinition exists only if some othergivendefinition is availablegiven orderingList[A](using ord: Ordering[A]): Ordering[List[A]] with ... -
extension methods for T are applicable if:
- visible in the current scope (defined, inherited, imported)
- defined in associated object of T
- defined in
giveninstance of T
trait Ordering[A]:def compare(x: A, y: A): Intextension (lhs: Rational)def < (rhs: A): Boolean = compare(lhs, rhs) < 0 -
implicit-conversion can be accomplished using
givenandscala.Coversion- at most one implicit conversion is done (cannot chain)
- requires
import scala.language.implicitConversions
Examples
Section titled “Examples”trait Serialize[T, O] { def serialize(t: T): O}implicit class SerializeOpt[A](val self: A) extends AnyVal { def serialize[O](implicit ser: Serialize[A, O]): O = ser.serialize(self)}
class Node(val value: String)object Node { type Json = String type Length = Int implicit val jsonSer: Serialize[Node, Json] = new Serialize[Node, Json] { def serialize(t: Node): Json = s"{'value': '${t.value}'}" } implicit val lengthSer: Serialize[Node, Length] = new Serialize[Node, Length] { def serialize(t: Node): Length = t.value.length }}import Node._new Node("hi").serialize[Json] // {'value': 'hi'}new Node("hi").serialize[Length] // 2(Co)Yoneda
Section titled “(Co)Yoneda”- Yoneda Converts a functor to a lazy functor such that
- multiple map invocations are deferred and composed
- when run, executes a single composed map on the underlying functor
- Coyoneda is similar to Yoneda except the enforcement that the underlying type needs to be a functor is enforced at run method instead of typeclass definition
- Creates a functor of any type constructor
Sthat takes a single parameter.Coyoneda[S[_], ?] - facilitates deriving a free monad which requires a functor
- Creates a functor of any type constructor
Free Monad
Section titled “Free Monad”-
intuition: represents a computation as a data structure
- In
Free[F, A]; thinkFreeas the program,Fas the algebra andAis the value produced by the program
sealed trait Console[A]case class ReadLine[A](value: String => A) extends Console[A]case class PrintLine[A](line: String, value: A) extends Console[A]type Dsl[A] = Free[Console, A]def readLine: Dsl[String] = ReadLine(identity)def printLine(line: String): Dsl[Unit] = PrintLine(line, ())val program =for {line <- readLine_ <- printLine("You wrote " + line)} yield () - In
-
data structures can be introspected, transformed etc
-
can have more than one interpreter to evaluate different results
Cofree (Monad)
Section titled “Cofree (Monad)”-
represents co-inductive rather than inductive process
-
inductive process starts far from base state and tries to reach a base, simpler state (e.g. given base state as
fact(1) = 0,fact(n) = n * fact(n-1)tries to reach the base state) -
co-inductive process starts from a base state and moves away from base state to generate other states (potentially infinite) E.g.
- given base states of
fib(0) = 0; fib(1) = 1generatefib(n) = fib(n-1) + fin(n-2) Stream.unfoldoffs2that generates a stream from an initial state
final case class Cofree[F[_], A](head: A, tail: F[Cofree[F, A]]) // F is a Functor - given base states of
-
headrepresents base state, andCofree[F, A]represents some state that is derived from the base state -
tailrepresents the recursive data structure, and it’s contained inFto indicate it is lazy, and can be infinite -
intuition #1:
Cofree[F,A]is a co-inductive process that generatesAs using effectF -
intuition #2:
Cofree[F,A]is a current postionAon a landscape that requires effectFto a new position. -
Cofreeare typically used to represent streams, that can be infinitetype Pipe[F[_], A, B] = Cofree[Kliesli[F, A, ?], B]type Source[F[_], A] = Pipe[F, Unit, A]type Sink[F[_], A] = Pipe[F, A, Unit]