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

Commit 7b9ad3a

Browse files
committed
Introduce MaybeOption
1 parent 53674ba commit 7b9ad3a

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

project/build.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ project.name=maybe
55
sbt.version=0.7.7
66
project.version=0.1-SNAPSHOT
77
publish.remote=false
8-
build.scala.versions=2.9.0 2.9.0-1
8+
build.scala.versions=2.9.1 2.9.0-1 2.9.0 2.8.1
99
project.initialize=false

project/build/Maybe.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ class Maybe(info: ProjectInfo) extends DefaultProject(info) {
3939

4040
// val lib_scalatest = "org.scalatest" %% "scalatest" % "1.4.1" % "test" withSources()
4141
val lib_scalatest = buildScalaVersion match {
42-
case "2.9.0" => "org.scalatest" % "scalatest_2.9.0" % "1.6.1" % "test" withSources()
43-
case "2.9.0-1" => "org.scalatest" % "scalatest_2.9.0" % "1.6.1" % "test" withSources()
42+
case "2.8.1" => "org.scalatest" % "scalatest_2.8.1" % "1.5.1" % "test" withSources()
43+
case "2.9.0" => "org.scalatest" % "scalatest_2.9.0" % "1.6.1" % "test" withSources()
44+
case "2.9.0-1" => "org.scalatest" % "scalatest_2.9.0-1" % "1.6.1" % "test" withSources()
45+
case "2.9.1" => "org.scalatest" % "scalatest_2.9.1" % "1.6.1" % "test" withSources()
4446
case v => error("Unsupported Scala version " + v)
4547
}
4648

src/main/scala/com/ckkloverdos/maybe/Maybe.scala

+22-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ sealed abstract class Maybe[+A] extends Equals {
7373

7474
def canEqual(that: Any): Boolean = that match {
7575
case _: Maybe[_] => true
76-
case _ => false
76+
case _ => false
7777
}
7878

7979
override def equals(that: Any) = that match {
@@ -84,8 +84,24 @@ sealed abstract class Maybe[+A] extends Equals {
8484
protected def equalsImpl(that: Maybe[_]): Boolean
8585
}
8686

87+
/**
88+
* A Maybe that can be either a Just or a NoVal. So, this is like Scala's Option
89+
*/
90+
sealed abstract class MaybeOption[+A] extends Maybe[A] {
91+
override def canEqual(that: Any): Boolean = that match {
92+
case _: MaybeOption[_] => true
93+
case _ => false
94+
}
95+
}
96+
97+
object MaybeOption {
98+
def apply[A](x: => A): MaybeOption[A] = Maybe(x) match {
99+
case j@Just(_) => j
100+
case _ => NoVal
101+
}
102+
}
103+
87104
object Maybe {
88-
type AnyFailure = Failed
89105
val MaybeEmptyIterator : Iterator [Nothing] = Iterator ()
90106
val MaybeEmptyTraversable: Traversable[Nothing] = Traversable()
91107
val MaybeEmptyList : List [Nothing] = List ()
@@ -100,20 +116,20 @@ object Maybe {
100116
* This is a polymorphic constructor for Maybes.
101117
* Use it if you are not sure what to expect from `x`.
102118
*/
103-
def apply[A](x: => A) = {
119+
def apply[A](x: => A): Maybe[A] = {
104120
try {
105121
val value = x
106122
value match {
107123
case null => NoVal
108124
case _ => Just(value)
109125
}
110126
} catch {
111-
case e: Throwable => Failed(e, "Maybe() failed")
127+
case e: Throwable => Failed(e, "Maybe.apply()")
112128
}
113129
}
114130
}
115131

116-
final case class Just[@specialized(Boolean, Char, Int, Double) +A](get: A) extends Maybe[A] {
132+
final case class Just[@specialized(Boolean, Char, Int, Double) +A](get: A) extends MaybeOption[A] {
117133
def toIterator = Iterator(get)
118134
def toTraversable = Traversable(get)
119135
def toOption = Some(get)
@@ -140,7 +156,7 @@ final case class Just[@specialized(Boolean, Char, Int, Double) +A](get: A) exten
140156
}
141157
}
142158

143-
case object NoVal extends Maybe[Nothing] {
159+
case object NoVal extends MaybeOption[Nothing] {
144160
def toIterator = Maybe.MaybeEmptyIterator
145161
def toTraversable = Maybe.MaybeEmptyTraversable
146162
def toOption = None

src/main/scala/com/ckkloverdos/package.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ package object maybe {
3939
}
4040

4141
@inline
42-
def safe[A](f: => A): Maybe[A] = Maybe(f)
42+
final def safe[A](f: => A): Maybe[A] = Maybe(f)
4343
}

0 commit comments

Comments
 (0)