Skip to content

Commit 22ca08d

Browse files
committed
LengthType: make it be a custom value class.
1 parent 5244c5c commit 22ca08d

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/strawman/collections/CollectionStrawMan2.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package strawman.collections
22

33
import Predef.{augmentString => _, wrapString => _, _}
4+
import scala.language.implicitConversions
45
import scala.reflect.ClassTag
56

67
/** A strawman architecture for new collections. It contains some
@@ -13,7 +14,21 @@ import scala.reflect.ClassTag
1314
*/
1415
object CollectionStrawMan1 {
1516

16-
type LengthType = Long
17+
type LengthTypeUnderlying = Long
18+
19+
final case class LengthType(u: LengthTypeUnderlying) extends AnyVal {
20+
def +(other: LengthType) = LengthType(u + other.u)
21+
def -(other: LengthType) = LengthType(u - other.u)
22+
def /(other: LengthType) = LengthType(u / other.u)
23+
def *(other: LengthType) = LengthType(u * other.u)
24+
def <(other: LengthType) = u < other.u
25+
def >(other: LengthType) = u > other.u
26+
def <=(other: LengthType) = u <= other.u
27+
def >=(other: LengthType) = u >= other.u
28+
}
29+
30+
implicit def intToLengthType(x: Int): LengthType = LengthType(x)
31+
implicit def lengthTypeToLong(x: LengthType): Long = x.u.toLong
1732

1833
/* ------------ Base Traits -------------------------------- */
1934

0 commit comments

Comments
 (0)