-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3322c72
Showing
15 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Release | ||
on: | ||
push: | ||
branches: [cli] | ||
tags: ["*"] | ||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: olafurpg/setup-scala@v10 | ||
- uses: coursier/cache-action@v3 | ||
- uses: olafurpg/setup-gpg@v3 | ||
- run: sbt ci-release | ||
env: | ||
PGP_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }} | ||
PGP_SECRET: ${{ secrets.PGP_SECRET }} | ||
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} | ||
SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: build | ||
on: [push, pull_request] | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: olafurpg/setup-scala@v10 | ||
- uses: coursier/cache-action@v3 | ||
- run: sbt clean main/headerCheck compile test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
*.class | ||
*.log | ||
|
||
# these are moved into the doc project by the build | ||
modules/docs/src/main/tut/changelog.md | ||
modules/docs/src/main/tut/license.md | ||
|
||
# sbt specific | ||
dist/* | ||
target/ | ||
lib_managed/ | ||
src_managed/ | ||
project/boot/ | ||
project/plugins/project/ | ||
project/hydra.sbt | ||
|
||
# Scala-IDE specific | ||
.scala_dependencies | ||
.cache | ||
.classpath | ||
.project | ||
.worksheet/ | ||
.settings/ | ||
|
||
# OS X | ||
.DS_Store | ||
|
||
# Ctags | ||
.tags | ||
|
||
# ENSIME | ||
.ensime | ||
.ensime_cache/ | ||
|
||
# IntelliJ | ||
.idea/ | ||
|
||
# Mill | ||
out/ | ||
|
||
# Bloop/Metals/vscode | ||
.bloop/ | ||
.metals/ | ||
.vscode/ | ||
metals.sbt | ||
|
||
# Transient workspace | ||
modules/docs/src/main/scala/ | ||
*.worksheet.sc | ||
|
||
# bsp | ||
.bsp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2020 by Rob Norris | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# 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.0-M1**. | ||
|
||
|
||
```scala | ||
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. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Our Scala versions. | ||
lazy val `scala-3.0` = "3.0.0-M1" | ||
lazy val `scala-2.12` = "2.12.12" | ||
lazy val `scala-2.13` = "2.13.3" | ||
|
||
// Publishing | ||
name := "typename" | ||
organization := "org.tpolecat" | ||
licenses ++= Seq(("MIT", url("http://opensource.org/licenses/MIT"))) | ||
homepage := Some(url("https://github.com/tpolecat/typename")) | ||
developers := List( | ||
Developer("tpolecat", "Rob Norris", "[email protected]", url("http://www.tpolecat.org")) | ||
) | ||
|
||
// Headers | ||
headerMappings := headerMappings.value + (HeaderFileType.scala -> HeaderCommentStyle.cppStyleLineComment) | ||
headerLicense := Some(HeaderLicense.Custom( | ||
"""|Copyright (c) 2020 by Rob Norris | ||
|This software is licensed under the MIT License (MIT). | ||
|For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|""".stripMargin | ||
) | ||
) | ||
|
||
// Compilation | ||
scalaVersion := `scala-2.13` | ||
crossScalaVersions := Seq(`scala-2.12`, `scala-2.13`, `scala-3.0`) | ||
Compile / doc / scalacOptions --= Seq("-Xfatal-warnings") | ||
Compile / doc / scalacOptions ++= Seq( | ||
"-groups", | ||
"-sourcepath", (baseDirectory in LocalRootProject).value.getAbsolutePath, | ||
"-doc-source-url", "https://github.com/tpolecat/typename/blob/v" + version.value + "€{FILE_PATH}.scala", | ||
) | ||
|
||
// MUnit | ||
libraryDependencies += "org.scalameta" %% "munit" % "0.7.18" % Test | ||
testFrameworks += new TestFramework("munit.Framework") | ||
|
||
// Scala 2 needs scala-reflect | ||
libraryDependencies ++= Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value).filterNot(_ => isDotty.value) | ||
|
||
// Add some more source directories | ||
unmanagedSourceDirectories in Compile ++= { | ||
val sourceDir = (sourceDirectory in Compile).value | ||
CrossVersion.partialVersion(scalaVersion.value) match { | ||
case Some((3, _)) => Seq(sourceDir / "scala-3") | ||
case Some((2, _)) => Seq(sourceDir / "scala-2") | ||
case _ => Seq() | ||
} | ||
} | ||
|
||
// Also for test | ||
unmanagedSourceDirectories in Test ++= { | ||
val sourceDir = (sourceDirectory in Test).value | ||
CrossVersion.partialVersion(scalaVersion.value) match { | ||
case Some((3, _)) => Seq(sourceDir / "scala-3") | ||
case Some((2, _)) => Seq(sourceDir / "scala-2") | ||
case _ => Seq() | ||
} | ||
} | ||
|
||
// dottydoc really doesn't work at all right now | ||
Compile / doc / sources := { | ||
val old = (Compile / doc / sources).value | ||
if (isDotty.value) | ||
Seq() | ||
else | ||
old | ||
} | ||
|
||
enablePlugins(AutomateHeaderPlugin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=1.4.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.4") | ||
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.6.0") | ||
addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % "0.8.0") | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "1.4.1") | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.6.3") | ||
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.5.1") | ||
addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.1.14") | ||
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.2.12") | ||
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1") | ||
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.4.6") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.4.3") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2020 by Rob Norris | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package org.tpolecat.typename | ||
|
||
import scala.reflect.macros.blackbox.Context | ||
|
||
trait TypeNamePlatform { | ||
|
||
implicit def typeName[T]: TypeName[T] = | ||
macro TypeNamePlatform.typeName_impl[T] | ||
|
||
} | ||
|
||
object TypeNamePlatform { | ||
|
||
def typeName_impl[T](c: Context): c.Expr[TypeName[T]] = { | ||
import c.universe._ | ||
val TypeApply(_, List(typeTree)) = c.macroApplication | ||
c.Expr(q"org.tpolecat.typename.TypeName(${typeTree.toString})") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2020 by Rob Norris | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package org.tpolecat.typename | ||
|
||
import scala.quoted._ | ||
|
||
trait TypeNamePlatform { | ||
|
||
transparent inline given instance[A] as TypeName[A] = ${TypeNamePlatform.impl[A]} | ||
|
||
} | ||
|
||
object TypeNamePlatform { | ||
|
||
def impl[A](using t: Type[A], ctx: QuoteContext): Expr[TypeName[A]] = { | ||
'{TypeName[A](${Expr(t.show)})} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright (c) 2020 by Rob Norris | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package org.tpolecat.typename | ||
|
||
final case class TypeName[A](value: String) | ||
object TypeName extends TypeNamePlatform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2020 by Rob Norris | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package org.tpolecat | ||
|
||
package object typename { | ||
|
||
def typeName[A](implicit ev: TypeName[A]): String = | ||
ev.value | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2020 by Rob Norris | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package test | ||
|
||
import org.tpolecat.typename._ | ||
import java.util.TreeMap | ||
import java.net.URI | ||
|
||
class Test extends munit.FunSuite { | ||
|
||
test("ground type") { | ||
assertEquals(typeName[String], "String") | ||
} | ||
|
||
test("parameterized type") { | ||
assertEquals(typeName[List[Int]], "List[Int]") | ||
} | ||
|
||
test("array (historically problematic)") { | ||
assertEquals(typeName[Array[Int]], "Array[Int]") | ||
} | ||
|
||
test("abstracted") { | ||
def foo[A: TypeName]: String = s"The name is ${typeName[A]}" | ||
assertEquals(foo[Array[Double]], "The name is Array[Double]") | ||
} | ||
|
||
test("fully-qualified") { | ||
assertEquals(typeName[TreeMap[ClassLoader, URI]], "java.util.TreeMap[ClassLoader,java.net.URI]") | ||
} | ||
|
||
test("wildcard (shorthand)") { | ||
assertEquals(typeName[List[_]], "List[_]") | ||
} | ||
|
||
test("wildcard (as forSome)") { | ||
assertEquals(typeName[List[a] forSome { type a }], "List[_]") | ||
} | ||
|
||
test("bounded wildcard (shorthand)") { | ||
assertEquals(typeName[List[_ <: ClassLoader]], "List[_ <: ClassLoader]") | ||
} | ||
|
||
test("bounded wildcard (as forSome)") { | ||
assertEquals(typeName[List[a] forSome { type a <: ClassLoader }], "List[_ <: ClassLoader]") | ||
} | ||
|
||
test("non-wildcard existential") { | ||
assertEquals(typeName[(A, Ordering[A]) forSome { type A }], "(A, Ordering[A])( forSome { type A })") | ||
} | ||
|
||
test("dependent existential") { | ||
assertEquals(typeName[List[a.B] forSome { val a: { type B }}], "List[a.B]( forSome { val a: AnyRef{type B} })") | ||
} | ||
|
||
} |
Oops, something went wrong.