Skip to main content

extras-type-info - Scala 2

types

import extras.typeinfo.syntax.types._

nestedTypeName

import extras.typeinfo.syntax.types._

sealed trait Foo
object Foo {
case object Bar extends Foo
final case class Baz(n: Int) extends Foo

def bar: Foo = Bar
def baz(n: Int): Foo = Baz(n)
}

Foo.Bar.nestedTypeName
// res1: String = "Foo.Bar"
Foo.Baz(1).nestedTypeName
// res2: String = "Foo.Baz"

Foo.bar.nestedTypeName
// res3: String = "MdocApp0.Foo"
Foo.baz(1).nestedTypeName
// res4: String = "MdocApp0.Foo"

Foo.Bar.nestedRuntimeClassName
// res5: String = "Foo.Bar"
Foo.Baz(1).nestedRuntimeClassName
// res6: String = "Foo.Baz"

Foo.bar.nestedRuntimeClassName
// res7: String = "Foo.Bar"
Foo.baz(1).nestedRuntimeClassName
// res8: String = "Foo.Baz"
import io.estatico.newtype.macros.newtype
import eu.timepit.refined.auto._
import eu.timepit.refined.types.string.NonEmptyString
import extras.typeinfo.syntax.types._

object data {
final case class Person(firstName: Person.FirstName, lastName: Person.LastName)

object Person {

@newtype case class FirstName(value: NonEmptyString)
@newtype case class LastName(value: NonEmptyString)

}
}

import data._

val firstName = Person.FirstName(NonEmptyString("Kevin"))
// firstName: Person.FirstName = Kevin
val lastName = Person.LastName(NonEmptyString("Lee"))
// lastName: Person.LastName = Lee
val person = Person(firstName, lastName)
// person: Person = Person(firstName = Kevin, lastName = Lee)

firstName.nestedTypeName
// res10: String = "Person.FirstName"
lastName.nestedTypeName
// res11: String = "Person.LastName"
person.nestedTypeName
// res12: String = "data.Person"

firstName.nestedRuntimeClassName
// res13: String = "api.Refined"
lastName.nestedRuntimeClassName
// res14: String = "api.Refined"
person.nestedRuntimeClassName
// res15: String = "data.Person"

More about type-info

syntax for WeakTypeTag

value.nestedTypeName

import java.time._

sealed trait Status
object Status {
final case class InProgress(startedAt: Instant) extends Status
case object Done extends Status

def inProgress(startedAt: Instant): Status = InProgress(startedAt)
def done: Status = Done
}
import scala.reflect.runtime.universe._
import extras.typeinfo.syntax.types._

def infoWithWeakTypeTag[A: WeakTypeTag](a: A): Unit =
println(
s"""value: $a
| type: ${weakTypeTag[A].nestedTypeName}
|""".stripMargin)

infoWithWeakTypeTag(Status.inProgress(Instant.now()))
// value: InProgress(2024-01-28T11:43:26.275850Z)
// type: MdocApp16.Status
//
infoWithWeakTypeTag(Status.InProgress(Instant.now()))
// value: InProgress(2024-01-28T11:43:26.277330Z)
// type: Status.InProgress
//

infoWithWeakTypeTag(Status.done)
// value: Done
// type: MdocApp16.Status
//
infoWithWeakTypeTag(Status.Done)
// value: Done
// type: Status.Done
//

WeakTypeTag[A].nestedTypeName

import java.time._

sealed trait Status
object Status {
final case class InProgress(startedAt: Instant) extends Status
case object Done extends Status

def inProgress(startedAt: Instant): Status = InProgress(startedAt)
def done: Status = Done
}
import scala.reflect.runtime.universe._
import extras.typeinfo.syntax.types._

def infoWithWeakTypeTag[A](implicit weakTypeTag: WeakTypeTag[A]): Unit =
println(
s"""type: ${weakTypeTag.nestedTypeName}
|""".stripMargin)

infoWithWeakTypeTag[Status.InProgress]
// type: Status.InProgress
//
infoWithWeakTypeTag[Status.Done.type]
// type: Status.Done
//

println(weakTypeTag[Status.InProgress].nestedTypeName)
// Status.InProgress
println(weakTypeTag[Status.Done.type].nestedTypeName)
// Status.Done

Works for @newtype

It works for newtype as well.

If you use newtype and want to get the newtype name, WeakTypeTag syntax is what you should use since you can get the name of newtype with it.

An example showing that it works with @newtype:

import io.estatico.newtype.macros.newtype

object Types {
@newtype case class Id(value: Long)
@newtype case class Username(value: String)
}
import scala.reflect.runtime.universe._
import extras.typeinfo.syntax.types._
def infoWithClassTag[A](a: A)(implicit weakTypeTag: WeakTypeTag[A]): Unit =
println(
s"""value: $a
| type: ${weakTypeTag.nestedTypeName}
|""".stripMargin)

import Types._

infoWithClassTag(Id(1L))
// value: 1
// type: Types.Id
//
infoWithClassTag(Username("someuser"))
// value: someuser
// type: Types.Username
//

println(weakTypeTag[Id].nestedTypeName)
// Types.Id
println(weakTypeTag[Username].nestedTypeName)
// Types.Username

syntax for ClassTag

value.nestedRuntimeClassName

import java.time._

sealed trait Status
object Status {
final case class InProgress(startedAt: Instant) extends Status
case object Done extends Status

def inProgress(startedAt: Instant): Status = InProgress(startedAt)
def done: Status = Done
}
import extras.typeinfo.syntax.types._

def infoWithClassTag[A](a: A): Unit =
println(
s"""value: $a
| type: ${a.nestedRuntimeClassName}
|""".stripMargin)

infoWithClassTag(Status.inProgress(Instant.now()))
// value: InProgress(2024-01-28T11:43:26.293043Z)
// type: Status.InProgress
//
infoWithClassTag(Status.InProgress(Instant.now()))
// value: InProgress(2024-01-28T11:43:26.293741Z)
// type: Status.InProgress
//

infoWithClassTag(Status.done)
// value: Done
// type: Status.Done
//
infoWithClassTag(Status.Done)
// value: Done
// type: Status.Done
//
println(Status.inProgress(Instant.now()).nestedRuntimeClassName)
// Status.InProgress
println(Status.InProgress(Instant.now()).nestedRuntimeClassName)
// Status.InProgress

println(Status.done.nestedRuntimeClassName)
// Status.Done
println(Status.Done.nestedRuntimeClassName)
// Status.Done

ClassTag[A].nestedRuntimeClassName

import java.time._

sealed trait Status
object Status {
final case class InProgress(startedAt: Instant) extends Status
case object Done extends Status

def inProgress(startedAt: Instant): Status = InProgress(startedAt)
def done: Status = Done
}
import scala.reflect.{classTag, ClassTag}
import extras.typeinfo.syntax.types._

def infoWithClassTag[A](implicit classTag: ClassTag[A]): Unit =
println(
s"""type: ${classTag.nestedRuntimeClassName}
|""".stripMargin)

infoWithClassTag[Status.InProgress]
// type: Status.InProgress
//
infoWithClassTag[Status.Done.type]
// type: Status.Done
//
println(classTag[Status.InProgress].nestedRuntimeClassName)
// Status.InProgress
println(classTag[Status.Done.type].nestedRuntimeClassName)
// Status.Done

Do not use for @newtype

Do not use it for newtype.

If you use newtype and want to get the newtype name, ClassTag syntax is not the one you should use since you can get only the actual type not newtype. For @newtype, please use 'reflects syntax for WeakTypeTag'.

An example showing that it does not work with @newtype:

import io.estatico.newtype.macros.newtype

object Types {
@newtype case class Id(value: Long)
@newtype case class Username(value: String)
}
import extras.typeinfo.syntax.types._

def infoWithClassTag[A](a: A): Unit =
println(
s"""value: $a
| type: ${a.nestedRuntimeClassName}
|""".stripMargin)

import Types._

infoWithClassTag(Id(1L))
// value: 1
// type: lang.Long
//
infoWithClassTag(Username("someuser"))
// value: someuser
// type: lang.String
//

println(Id(1L).nestedRuntimeClassName)
// lang.Long
println(Username("someuser").nestedRuntimeClassName)
// lang.String