Scala With Cats Flashcards

1
Q

A Type Class is…

A

Interface or API that represents some functionality we want to implement.

Represented by a Trait with at least One Type Parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Type Class Instances

A

Provide implementations for the Types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Type Class Interface

A

Functionality exposed to users

Generic methods that accept instances of the Type Class as implicit parameters

  • Interface Objects: place methods in a singleton object
  • Interface Syntax: use extension methods to extend
    exiting Types with interface methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The ‘implicitly’ Method

A

Scala generic Type Class Interface

Summon any value from Implicit Scope

Note:
- Good fallback for debugging purposes, the compiler will ensure it can find an instance of the type and that there are not ambiguous

  • Type Classes in Scala means working with Implicit Values and Implicit Parameters
  • Implicit in Scala must be placed inside an Object or Trait
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Type Class Definitions are only included in Implicit Scope if …

A

They are tagged with the ‘implicit’ keyword

Note:

  • the compiler searches for candidate Type Class Instances by Type in the Implicit Scope at the call site
    • in local or inherited Definitions
    • in imported Definitions
    • in the Companion Object of the Type Class or the Parameter Type
  • if the compiler sees multiple candidate Definitions, it fails with an ambiguous implicit values error
  • The power of Type Classes and Implicit lies in the compiler’s ability to Combine Implicit Definitions when searching for candidate Instances
  • We can define Instances in two ways
    • By defining concrete Instances as Implicit Vals of the required Type
    • By defining Implicit Methods to construct Instances from other Type Class instances
      example: JsonWriter[A] and JsonWriter[Option[A]]
    • Be sure to mark the parameters to the method as implicit parameters.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Implicit methods with non-implicit parameters form a different Scala pattern called an …

A

Implicit Conversion (frowned upon)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Cats is written using a …

A

Modular Structure that allows us to choose which Type Classes, Instances, and Interface Methods we want to use.

Note:

  • import cats._ imports all of Cats’ Type Classes
  • import cats.instances.all._ imports all of the Type Class Instances for the Standard Pibrary
  • import cats.syntax.all._ imports all of the Syntax
  • import cats.implicits._ imports all of the Standard Type Class Instances and all of the Syntax
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When working with Type Classes we must consider two issues that control instance selection

A

The relationship between an Instance defined on a Type and its Subtypes

How do we choose between Type Class Instances when there are many available

How well did you know this?
1
Not at all
2
3
4
5
Perfectly