julianpeeters / typesize   0.1.0

Apache License 2.0 GitHub

A typeclass that provides the cardinalities of common types

Scala versions: 3.x
Scala.js versions: 1.x
Scala Native versions: 0.4

typesize

A typeclass that provides the number of inhabitants of common types.

Add the dependency:

  • libarary for Scala 3 (JS, JVM, and Native platforms)
"com.julianpeeters" %% "typesize" % "0.1.0"

Description:

Calling SizeOf[A].size returns an Int a Cardinality of three flavors:

  • Finite: backed by a BigInt
  • NonFinite: for types like List[A]
  • TooBigToCompute: for types like String

Built-in typeclass instance are provided for common primitive and complex types, calculated by the following operations:

  • +: take the sum of the members of a sealed trait
  • *: take the product of the members of a tuple
  • pow: take the exponentiation of the members of a function

Example:

import typesize.{Cardinality, SizeOf, given}

val c: Cardinality = summon[SizeOf[Option[Boolean]]].size
// c: Cardinality = Finite(i = 3)

Supported types:

click to expand
Primitive:
  • Boolean
  • Byte
  • Char (assumption: decoded from utf8, e.g. fs2.text)
  • Int
  • Long
  • Nothing
  • String
  • Unit
Complex:
  • Array
  • Either
  • Function (1-9)
  • List
  • Option
  • Tuple (2-9)