typename


License

License

MIT
GroupId

GroupId

org.tpolecat
ArtifactId

ArtifactId

typename_2.12
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

typename
typename
Project URL

Project URL

https://github.com/tpolecat/typename
Project Organization

Project Organization

org.tpolecat
Source Code Management

Source Code Management

https://github.com/tpolecat/typename

Download typename_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/org.tpolecat/typename_2.12/ -->
<dependency>
    <groupId>org.tpolecat</groupId>
    <artifactId>typename_2.12</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.tpolecat/typename_2.12/
implementation 'org.tpolecat:typename_2.12:1.0.0'
// https://jarcasting.com/artifacts/org.tpolecat/typename_2.12/
implementation ("org.tpolecat:typename_2.12:1.0.0")
'org.tpolecat:typename_2.12:jar:1.0.0'
<dependency org="org.tpolecat" name="typename_2.12" rev="1.0.0">
  <artifact name="typename_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.tpolecat', module='typename_2.12', version='1.0.0')
)
libraryDependencies += "org.tpolecat" % "typename_2.12" % "1.0.0"
[org.tpolecat/typename_2.12 "1.0.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.12
org.scala-lang : scala-reflect jar 2.12.12

test (1)

Group / Artifact Type Version
org.scalameta : munit_2.12 jar 0.7.26

Project Modules

There are no modules declared in this project.

TypeName

This is a Scala micro-library that provides:

  • a typeName[A] method that returns a string representation of A for all types in Scala 2, and for most concrete types in Scala 3; and
  • a TypeName[A] typeclass for demanding that a type has a name.

The intent is that you can use this instead of TypeTag or other heavy machinery.

TypeName is compiled for Scala 2.12, 2.13, and 3.0.

libraryDependencies += "org.tpolecat" %% "typename" % <version>

Discussion and Demo

The actual names that are generated depend on your Scala version and what's in scope. They're good for things like error reporting, but don't depend on them being stable through space (i.e., equivalent types may give different names, depending on what's in scope when you ask) or time (names seem to be the same in 2.12 and 2.13, but they're very different in 3.0).

Anyway it's still useful. In Scala 2 the results look a bit like this.

Welcome to Scala 2.13.3 (OpenJDK 64-Bit Server VM, Java 11.0.7).
Type in expressions for evaluation. Or try :help.

scala> import org.tpolecat.typename._
import org.tpolecat.typename._

scala> typeName[Map[String, ClassLoader]]
val res1: String = Map[String,ClassLoader]

scala> def foo[A](a: A)(implicit ev: TypeName[A]) = s"The type of $a is ${ev.value}"
def foo[A](a: A)(implicit ev: org.tpolecat.typename.TypeName[A]): String

scala> foo(1.23)
val res2: String = The type of 1.23 is Double

scala> foo(List(Some(1), None, Some(2)))
val res3: String = The type of List(Some(1), None, Some(2)) is List[Option[Int]]

In Scala 3 the results are a little different, but still fine.

cala> import org.tpolecat.typename._

scala> typeName[Map[String, ClassLoader]]
val res0: String = scala.collection.immutable.Map[scala.Predef.String, java.lang.ClassLoader]

scala> def foo[A](a: A)(implicit ev: TypeName[A]) = s"The type of $a is ${ev.value}"
def foo[A](a: A)(implicit ev: org.tpolecat.typename.TypeName[A]): String

scala> foo(1.23)
val res1: String = The type of 1.23 is scala.Double

scala> foo(List(Some(1), None, Some(2)))
val res2: String = The type of List(Some(1), None, Some(2)) is scala.collection.immutable.List[scala.Option[scala.Int]]

Known Limitations

In Scala 2 (this correctly doesn't work in Scala 3) you can ask for typeName[A] for an unconstrainted type variable A and it will happily tell you "A", which is almost certainly a bug if you're doing that. You need to introduce A like [A: TypeName] if you need its name.

In Scala 3 you sometimes can't get the names of nasty types, for example the type of List(Vector(1), Set(1)). I don't know what the limits are.

Versions

Version
1.0.0
0.1.7
0.1.6
0.1.5
0.1.3
0.1.2
0.1.1
0.1.0