Skip to main content

Reflects Syntax

reflects 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.reflects.syntax.tags._

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

infoWithWeakTypeTag(Status.inProgress(Instant.now()))
// value: InProgress(2022-07-22T10:17:07.257218Z)
// type: App0.Status
//
infoWithWeakTypeTag(Status.InProgress(Instant.now()))
// value: InProgress(2022-07-22T10:17:07.307637Z)
// type: Status.InProgress
//

infoWithWeakTypeTag(Status.done)
// value: Done
// type: App0.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.reflects.syntax.tags._

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.reflects.syntax.tags._
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

reflects 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.reflects.syntax.tags._

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

infoWithClassTag(Status.inProgress(Instant.now()))
// value: InProgress(2022-07-22T10:17:07.324132Z)
// type: Status.InProgress
//
infoWithClassTag(Status.InProgress(Instant.now()))
// value: InProgress(2022-07-22T10:17:07.326139Z)
// 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.reflects.syntax.tags._

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.reflects.syntax.tags._

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

reflects syntax for Class

value.nestedClassName

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.reflects.syntax.classes._

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

infoWithClass(Status.inProgress(Instant.now()))
// value: InProgress(2022-07-22T10:17:07.334734Z)
// type: Status.InProgress
//
infoWithClass(Status.InProgress(Instant.now()))
// value: InProgress(2022-07-22T10:17:07.335899Z)
// type: Status.InProgress
//

infoWithClass(Status.done)
// value: Done
// type: Status.Done
//
infoWithClass(Status.Done)
// value: Done
// type: Status.Done
//
println(Status.inProgress(Instant.now()).nestedClassName)
// Status.InProgress
println(Status.InProgress(Instant.now()).nestedClassName)
// Status.InProgress

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

Class[A].nestedClassName

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.reflects.syntax.classes._

def infoWithClass[A](aClass: Class[A]): Unit =
println(
s"""type: ${aClass.nestedClassName}
|""".stripMargin)

infoWithClass(Status.InProgress.getClass)
// type: Status.InProgress
//
infoWithClass(Status.Done.getClass)
// type: Status.Done
//

Do not use for @newtype

Do not use it for newtype.

If you use newtype and want to get the newtype name, Class 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.reflects.syntax.classes._

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

import Types._

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

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