Standard Given Instances
The StdLibGivens
trait provides standard Scala type class instances Numeric
, Integral
, Fractional
for newtype wrappers (only works for newtypes which do not have validation).
Examples
For the upcoming examples let's define some newtypes and values:
import yantl.*
object Score extends Newtype.WithoutValidationOf[Long] with StdLibGivens with math.Integral.ExtraImplicits
type Score = Score.Type
object ScoreFloat extends Newtype.WithoutValidationOf[Float] with StdLibGivens with math.Fractional.ExtraImplicits
type ScoreFloat = ScoreFloat.Type
val score1 = Score(50)
// score1: Type = 50L
val score2 = Score(100)
// score2: Type = 100L
val scoreFloat1 = ScoreFloat(50)
// scoreFloat1: Type = 50.0F
val scoreFloat2 = ScoreFloat(100)
// scoreFloat2: Type = 100.0F
Numeric
Brings +
, -
, *
operations:
val addition = score1 + score2
// addition: Type = 150L
val subtraction = score1 - score2
// subtraction: Type = -50L
val multiplication = score1 * score2
// multiplication: Type = 5000L
Integral
Brings /
and %
operations for natural numbers (like Int
, Long
)
val division = score1 / score2
// division: Type = 0L
val modulus = score1 % score2
// modulus: Type = 50L
Fractional
Brings /
operation for real numbers (like Float
, Double
)
val divisionFloat = scoreFloat1 / scoreFloat2
// divisionFloat: Type = 0.5F