Skip to main content
Version: v1.8.1

cats module

Import

import refined4s.modules.cats.derivation.*

note

The following import is required only for versions prior to 1.8.0.

import refined4s.modules.cats.derivation.types.all.given

If you are using refined4s 1.8.0 or later, you don't need it.

Use Drived Instances for Pre-defined Types

NOTE

Since refined4s 1.8.0, you no longer need to import anything to use the predefined Eq and Show type-class instances derived from the actual values. As soon as you project includes cats, refined4s will automatically provide the predefined Eq and Show type-class instances derived from the actual values.

NOTE

This works only when the actual type already has Eq and Show.

info

Eq and/or Show type-class instances are provided only for the types from refined4s.types.all. So if you want your Newtype or Refined or InlinedRefined to have Eq and Show instances,
you can use pre-defined traits for cats or the deriving method instead.

import refined4s.*

type Name = Name.Type
object Name extends Newtype[String]

type NotEmptyStr = NotEmptyStr.Type
object NotEmptyStr extends Refined[String] {
inline def invalidReason(a: String): String = "non-empty String"

inline def predicate(a: String): Boolean = a != ""
}

import cats.*
import cats.syntax.all.*

def hello[A: Show](a: A): Unit = println(show"Hello $a")

def equal[A: Eq](a1: A, a2: A): Unit =
if Eq[A].eqv(a1, a2) then println("The given values are equal.")
else println("The given values are not equal.")

For refined4s.types.all.*, Eq and Show type-class instances are already provided if the underlying type has Eq and Show instances.

e.g.)

import refined4s.types.all.*

hello(NonEmptyString("Peter Parker"))
// Hello Peter Parker

hello(PosInt(999))
// Hello 999


equal(NonEmptyString("Peter Parker"), NonEmptyString("Peter Parker"))
// The given values are equal.

equal(NonEmptyString("Peter Parker"), NonEmptyString("Natasha Romanoff"))
// The given values are not equal.

equal(NonNegInt(0), NonNegInt(0))
// The given values are equal.

equal(NegInt(-123), NegInt(-999))
// The given values are not equal.

Custom types without Eq and Show type-class instances,

hello(Name("Tony Stark"))
// error:
// No given instance of type cats.Show[repl.MdocSession.MdocApp.Name.Type] was found for a context parameter of method hello in object MdocApp
// hello(Name("Tony Stark"))
// ^
hello(NotEmptyStr("Thor Odinson"))
// error:
// No given instance of type cats.Show[repl.MdocSession.MdocApp.NotEmptyStr.Type] was found for a context parameter of method hello in object MdocApp
// hello(NotEmptyStr("Thor Odinson"))
// ^
equal(Name("Tony Stark"), Name("Tony Stark"))
// error:
// No given instance of type cats.kernel.Eq[repl.MdocSession.MdocApp.Name.Type] was found for a context parameter of method equal in object MdocApp
// equal(Name("Tony Stark"), Name("Tony Stark"))
// ^
equal(NotEmptyStr("Thor Odinson"), NotEmptyStr("Thor Odinson"))
// error:
// No given instance of type cats.kernel.Eq[repl.MdocSession.MdocApp.NotEmptyStr.Type] was found for a context parameter of method equal in object MdocApp
// equal(NotEmptyStr("Thor Odinson"), NotEmptyStr("Thor Odinson"))
// ^

With Explicit Pre-defined Cats Support

There are the following pre-defined traits to support cats' Eq and Show.

  • refined4s.modules.cats.derivation.CatsEq
  • refined4s.modules.cats.derivation.CatsShow
  • refined4s.modules.cats.derivation.CatsEqShow
NOTE

This works only when the actual type already has Eq and Show.

import refined4s.*
import refined4s.modules.cats.derivation.*

type Name = Name.Type
object Name extends Newtype[String] with CatsEqShow[String]

type NotEmptyStr = NotEmptyStr.Type
object NotEmptyStr extends Refined[String] with CatsEqShow[String] {
inline def invalidReason(a: String): String = "non-empty String"

inline def predicate(a: String): Boolean = a != ""
}

import cats.*
import cats.syntax.all.*

def hello[A: Show](a: A): Unit = println(show"Hello $a")

def equal[A: Eq](a1: A, a2: A): Unit =
if Eq[A].eqv(a1, a1) then println("The given values are equal.")
else println("The given values are not equal.")
hello(Name("Tony Stark"))
// Hello Tony Stark

hello(NotEmptyStr("Thor Odinson"))
// Hello Thor Odinson

equal(Name("Tony Stark"), Name("Tony Stark"))
// The given values are equal.
equal(Name("Tony Stark"), Name("Steve Rogers"))
// The given values are not equal.

equal(NotEmptyStr("Thor Odinson"), NotEmptyStr("Thor Odinson"))
// The given values are equal.
equal(NotEmptyStr("Thor Odinson"), NotEmptyStr("Bruce Banner"))
// The given values are not equal.

With deriving Method

If you want to have explicit Eq and Show type-class instances in your Newtype or Refined or InlinedRefined, you can use the deriving method.

NOTE

This works only when the actual type already has Eq and Show.

import cats.*

import refined4s.*
import refined4s.modules.cats.derivation.*

type Name = Name.Type
object Name extends Newtype[String] {
given eqName: Eq[Name] = deriving[Eq]
given showName: Show[Name] = deriving[Show]
}

type NotEmptyStr = NotEmptyStr.Type
object NotEmptyStr extends Refined[String] with CatsEqShow[String] {
inline def invalidReason(a: String): String = "non-empty String"

inline def predicate(a: String): Boolean = a != ""

given eqNotEmptyStr: Eq[NotEmptyStr] = deriving[Eq]
given showNotEmptyStr: Show[NotEmptyStr] = deriving[Show]
}

import cats.*
import cats.syntax.all.*

def hello[A: Show](a: A): Unit = println(show"Hello $a")

def equal[A: Eq](a1: A, a2: A): Unit =
if Eq[A].eqv(a1, a1) then println("The given values are equal.")
else println("The given values are not equal.")
hello(Name("Tony Stark"))
// Hello Tony Stark

hello(NotEmptyStr("Thor Odinson"))
// Hello Thor Odinson

equal(Name("Tony Stark"), Name("Tony Stark"))
// The given values are equal.
equal(Name("Tony Stark"), Name("Steve Rogers"))
// The given values are not equal.

equal(NotEmptyStr("Thor Odinson"), NotEmptyStr("Thor Odinson"))
// The given values are equal.
equal(NotEmptyStr("Thor Odinson"), NotEmptyStr("Bruce Banner"))
// The given values are not equal.