Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit df4cbac

Browse files
author
Fulvio Valente
committed
Allocate only one Label per tag type, and only one default Label.
1 parent d2f2d84 commit df4cbac

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/main/scala/Label.scala

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,28 @@ trait Label[A] {
66
}
77

88
object Label {
9-
private[id] def default[A] = new Label[A] {
10-
def label = ""
9+
private val defaultLabel: Label[Nothing] = new Label[Nothing] {
10+
val label = ""
1111
}
1212

13+
private[id] def default[A]: Label[A] = defaultLabel.asInstanceOf[Label[A]]
14+
1315
private[id] sealed trait LabelDefinitionConflict
1416

17+
private def nameOf[A](implicit m: Manifest[A]) = {
18+
val typeName = m.toString
19+
typeName.substring(0, typeName.lastIndexOf(".type"))
20+
}
21+
1522
trait MakeLabel { self =>
1623
// See eidos.id.Format.UUID for an explanation of this
1724
// format: off
1825
final def `"In Eidos, you can only extend one of MakeLabel or CustomLabel"`
1926
: LabelDefinitionConflict = null
2027

21-
implicit final def l(implicit ev: self.type <:< Product): Label[this.type] =
22-
new Label[this.type] {
23-
def label = self.productPrefix
24-
}
28+
implicit final val l: Label[this.type] = new Label[this.type] {
29+
val label: String = nameOf[self.type]
30+
}
2531
}
2632

2733
trait CustomLabel {
@@ -30,10 +36,10 @@ object Label {
3036
// format: on
3137
def label: String
3238

33-
private def customLabel = label
39+
private val customLabel = label
3440

35-
implicit final def l: Label[this.type] = new Label[this.type] {
36-
def label = customLabel
41+
implicit final val l: Label[this.type] = new Label[this.type] {
42+
val label = customLabel
3743
}
3844
}
3945
}

src/test/scala/EidosSpec.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ class EidosSpec extends Specification with TypecheckMatchers with ScalaCheck {
8383
"Tag" should {
8484
"be case objects" in {
8585
trait Trait
86-
87-
// MakeLabel is the reason why "case" is required
88-
object Object extends MakeLabel
86+
class Class
87+
object Object
8988
type Object = Object.type
9089

9190
case object CaseObject
@@ -96,6 +95,7 @@ class EidosSpec extends Specification with TypecheckMatchers with ScalaCheck {
9695

9796
// format: off
9897
{ typecheck("""Id.of[Trait]("")""") must failWith(errorMessage("Trait")) }
98+
{ typecheck("""Id.of[Class]("")""") must failWith(errorMessage("Class")) }
9999
{ typecheck("""Id.of[Object]("")""") must failWith(errorMessage("Object")) }
100100
{ typecheck("""Id.of[CaseObject]("")""") must succeed }
101101
// format: on

0 commit comments

Comments
 (0)