Skip to content
This repository was archived by the owner on Aug 17, 2019. It is now read-only.

Commit 86004bc

Browse files
committed
Better semantics of castTo
1 parent fdf1a1a commit 86004bc

File tree

2 files changed

+11
-29
lines changed

2 files changed

+11
-29
lines changed

Diff for: src/main/scala/com/ckkloverdos/maybe/Maybe.scala

+5-16
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ sealed abstract class Maybe[+A] {
105105
final def forFailed[B >: A](f: Failed Maybe[B]): Maybe[B] =
106106
if(isFailed) f(this.asInstanceOf[Failed]) else this
107107

108-
def safeCastTo[B: Manifest]: MaybeOption[B]
109-
110-
def castTo[B: Manifest]: Maybe[B]
108+
def castTo[B <: AnyRef : Manifest]: Maybe[B]
111109

112110
/**
113111
* Flattens two successive maybes to one.
@@ -198,15 +196,10 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
198196
def filter(f: (A) Boolean): Maybe[A] = if(f(get)) this else NoVal
199197
def foreach(f: A Unit) = f(get)
200198

201-
def safeCastTo[B: Manifest] = get match {
202-
case null NoVal // normally null should not even be here but we are being cautious
203-
case value if(manifest[B].erasure.isInstance(value)) this.asInstanceOf[MaybeOption[B]] else NoVal
204-
}
205-
206-
def castTo[B: Manifest] = get match {
199+
def castTo[B <: AnyRef : Manifest] = get match {
207200
case null NoVal
208201
case value if(manifest[B].erasure.isInstance(value)) this.asInstanceOf[Maybe[B]]
209-
case value Failed(new ClassCastException("%s -> %s".format(get.asInstanceOf[AnyRef].getClass.getName, manifest[B])))
202+
case value Failed(new ClassCastException("%s -> %s".format(get.getClass.getName, manifest[B].erasure.getName)))
210203
}
211204

212205
def flatten1[U](implicit ev: A <:< Maybe[U]): Maybe[U] = ev(get)
@@ -240,9 +233,7 @@ case object NoVal extends MaybeOption[Nothing] {
240233

241234
def finallyFlatMap[B](_finally: (Nothing) Unit)(f: (Nothing) Maybe[B]) = this
242235

243-
def safeCastTo[B: Manifest]: MaybeOption[B] = this
244-
245-
def castTo[B: Manifest] = this
236+
def castTo[B <: AnyRef : Manifest] = this
246237

247238
def flatten1[U](implicit ev: <:<[Nothing, Maybe[U]]) = this
248239

@@ -280,9 +271,7 @@ final case class Failed(cause: Throwable) extends MaybeEither[Nothing] {
280271

281272
def finallyFlatMap[B](_finally: (Nothing) Unit)(f: (Nothing) Maybe[B]) = this
282273

283-
def safeCastTo[B: Manifest]: MaybeOption[B] = NoVal
284-
285-
def castTo[B: Manifest] = this
274+
def castTo[B <: AnyRef : Manifest] = this
286275

287276
def flatten1[U](implicit ev: <:<[Nothing, Maybe[U]]) = this
288277

Diff for: src/test/scala/com/ckkloverdos/maybe/MaybeTest.scala

+6-13
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,25 @@ class MaybeTest {
9494

9595
@Test
9696
def testCastNullToNoVal: Unit = {
97-
Assert.assertEquals(NoVal, Just(null).safeCastTo[Int])
97+
Assert.assertEquals(NoVal, Just(null).castTo[String])
9898
}
9999

100100
@Test
101-
def testSafeCastTo: Unit = {
101+
def testCastTo: Unit = {
102102
val help = "Help"
103103
val aJust: Maybe[_] = Just(help)
104-
Assert.assertTrue(aJust.safeCastTo[CharSequence].isJust)
104+
Assert.assertTrue(aJust.castTo[CharSequence].isJust)
105105
}
106106

107107
@Test
108-
def testSafeCastTo2: Unit = {
108+
def testCastTo2: Unit = {
109109
val help = "Help"
110110
val aJust: Maybe[_] = Just(help)
111-
Assert.assertFalse(aJust.safeCastTo[Int].isJust)
111+
Assert.assertTrue(aJust.castTo[ScalaObject].isFailed)
112112
}
113113

114114
@Test
115-
def testCastTo: Unit = {
116-
println("Just(1).castTo[Int] = %s".format(Just(1).castTo[Int]))
117-
Assert.assertTrue(Just(1).castTo[Int].isJust)
118-
}
119-
120-
@Test
121-
def testCastTo2: Unit = {
122-
println("Just(1).castTo[String] = %s".format(Just(1).castTo[String]))
115+
def testCastTo3: Unit = {
123116
Assert.assertTrue(Just(1).castTo[String].isFailed)
124117
}
125118

0 commit comments

Comments
 (0)