Skip to main content

cats module

Import

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

Use Drived Instances for Pre-defined Types

To make Newtype, Refined and InlinedRefined have Eq and Show type-class instances derived from the actual values, you can simply use

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

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

info

Using refined4s.modules.cats.derivation.types.all.given is required only when Eq and/or Show is used.
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, a1) then println("The given values are equal.")
else println("The given values are not equal.")

With derivation.types.all,

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

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

hello(Name("Tony Stark"))
// Hello Tony Stark

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

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(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.

Without derivation.types.all,

hello(NonEmptyString("Kevin"))
// error:
// Not found: NonEmptyString
// def hello[A: Show](a: A): Unit = println(show"Hello $a")
// ^
// error:
// ambiguous given instances: both method catsShowForUUID in object Show and method catsShowForBigDecimal in object Show match type cats.Show[Any] of an implicit parameter of method hello in object MdocApp
// def equal[A: Eq](a1: A, a2: A): Unit =
//
hello(Name("Kevin"))
// error:
// no given instance of type cats.Show[repl.MdocSession.MdocApp.Name.Type] was found for an implicit parameter of method hello in object MdocApp
// inline def predicate(a: String): Boolean = a != ""
//
hello(NotEmptyStr("Kevin"))
// error:
// no given instance of type cats.Show[repl.MdocSession.MdocApp.NotEmptyStr.Type] was found for an implicit parameter of method hello in object MdocApp
// def hello[A: Show](a: A): Unit = println(show"Hello $a")
// ^
equal(NonEmptyString("Kevin"), NonEmptyString("Kevin"))
// error:
// Not found: NonEmptyString
// inline def predicate(a: String): Boolean = a != ""
// ^
// error:
// Not found: NonEmptyString
// def hello[A: Show](a: A): Unit = println(show"Hello $a")
// ^
// error:
// ambiguous given instances: both method catsKernelInstancesForDuration in object Eq and method catsKernelInstancesForBitSet in object Eq match type cats.kernel.Eq[Any] of an implicit parameter of method equal in object MdocApp
// def equal[A: Eq](a1: A, a2: A): Unit =
//
equal(Name("Kevin"), Name("Kevin"))
// error:
// no given instance of type cats.kernel.Eq[repl.MdocSession.MdocApp.Name.Type] was found for an implicit parameter of method equal in object MdocApp
equal(NotEmptyStr("Kevin"), NotEmptyStr("Kevin"))
// error:
// no given instance of type cats.kernel.Eq[repl.MdocSession.MdocApp.NotEmptyStr.Type] was found for an implicit parameter of method equal in object MdocApp
// def equal[A: Eq](a1: A, a2: A): Unit =
//

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.