I'm trying to implement an Option which doesn't consume extra memory for a wrapper. I create a class. Null stands for None, non-null value stands for Some.
class Maybe[+T](private val nullable: T) extends AnyVal {
@inline final def isDefined: Boolean = {
println("isDefined called")
val res = nullable != null
println(s"result = $res")
res
}
@inline final def get: T = if (isDefined) nullable else throw new NoSuchElementException
@inline final def getOrElse[B >: T](default: => B): B = if (isDefined) this.get else default
@inline final def map[B](f: T => B) : Maybe[B] = if (isDefined) Maybe.some(f(this.get)) else Maybe.none
@inline final def flatMap[B](f: T => Maybe[B]): Maybe[B] = if (!isDefined) Maybe.none else f(get)
def toOption : Option[T] = if (isDefined) Some(get) else None
}
object Maybe {
def some[T](value:T) : Maybe[T] = new Maybe(value)
final val none : Maybe[Nothing] = {
println("start initializing none")
val v = new Maybe(null)
println("1")
val res = v.asInstanceOf[Maybe[Nothing]]
println("2")
res
}
}
object MaybeTest extends App {
println("try to create Maybe.none")
val myNone = Maybe.none
println("Initialized")
println(myNone.isDefined)
println("Accessed")
}
But when I try to run it I get an NPE:
try to create Maybe.none
start initializing none
1
2
Initialized
Exception in thread "main" java.lang.NullPointerException at com.MaybeTest$.delayedEndpoint$com$MaybeTest$1(Maybe.scala:34) at com.MaybeTest$delayedInit$body.apply(Maybe.scala:30) at scala.Function0$class.apply$mcV$sp(Function0.scala:34) at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) at scala.App$$anonfun$main$1.apply(App.scala:76) at scala.App$$anonfun$main$1.apply(App.scala:76) at scala.collection.immutable.List.foreach(List.scala:392) at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35) at scala.App$class.main(App.scala:76) at com.MaybeTest$.main(Maybe.scala:30) at com.MaybeTest.main(Maybe.scala)
If I remove 'extends AnyVal' everything works fine. Could anyone explain such behavior?
@Jasper-M's comment explains why your solution can't work, but this is close:
class Maybe[+T] private (private val nullable: Object) extends AnyVal {
@inline private final def get0 = nullable.asInstanceOf[T]
@inline final def isDefined: Boolean = nullable != null
@inline final def get: T = if (isDefined) get0 else throw new NoSuchElementException
@inline final def getOrElse[B >: T](default: => B): B = if (isDefined) get0 else default
@inline final def map[B](f: T => B) : Maybe[B] = if (isDefined) Maybe.some(f(get0)) else Maybe.none
@inline final def flatMap[B](f: T => Maybe[B]): Maybe[B] = if (!isDefined) Maybe.none else f(get0)
@inline final def toOption: Option[T] = if (isDefined) Some(get0) else None
}
object Maybe {
def some[A](value: A) : Maybe[A] = {
val value1 = value.asInstanceOf[Object]
assert(value1 != null)
new Maybe[A](value1)
}
private val _none = new Maybe[Null](null)
def none[A]: Maybe[A] = _none.asInstanceOf[Maybe[A]]
def apply[A](value: A) = new Maybe[A](value.asInstanceOf[Object])
}
But it isn't really "an Option
which doesn't consume extra memory for a wrapper": it's just calling null
by another name. E.g. Option[Option[A]]
works as expected, but Maybe[Maybe[A]]
doesn't (and the compiler won't tell you!).