According to the docs, None object is intended to “represent non-existent values”. As far as I’ve seen it’s mostly used as an empty Option. But do you think it’s a good idea to use it for other purposes. For example, in my library I want to have an universal “Empty” object which could be assigned for various missing values, where I would just implicitly convert the “Empty” value to my types as needed:
// In library:
trait A {
implicit def noneToT1(none: Option[Nothing]): T1 = defaultT1
implicit def noneToT2(none: Option[Nothing]): T2 = defaultT2
def f1: T1
def f2: T2
}
// In the code that uses the library
class EmptyA extends A {
def f1 = None
def f2 = None
}
One reason for not (mis)using None in this fashion is that the user would expect that f1 and f2 return Option[T1] and Option[T2] respectively. And they don’t. Off course, I could have def f1: Option[T1], but in this case the values are not actually optional, they just can have some default empty value, or a real value, I just want to create the default values “under the hood” and have some uniform way of saying “default” or “empty” through the entire library. So the question is, should I use None to express this “defaultness” or go for some custom type? Right now I’m using my own object Empty, but it feels a bit superfluous.
EDIT:
To ilustrate my question I’ll add the code I am using right now:
// In library:
trait Empty
object Empty extends Empty
trait A {
implicit def emptyToT1(none: Empty): T1 = defaultT1
implicit def emptyToT2(none: Empty): T2 = defaultT2
def f1: T1
def f2: T2
}
// In the code that uses the library
class EmptyA extends A {
def f1 = Empty
def f2 = Empty
}
class HalfFullA extends A {
def f1 = Empty
def f2 = someValue2
}
class FullA extends A {
def f1 = someValue1
def f2 = someValue2
}
My question is quite simple: is it a good idea to use scala’s None instead of my Empty?
If you can’t or don’t want to define the default value using inheritance, I suggest to keep the new object. Reusing
Nonefor something else than theSomecounterpart seems wrong and doesn’t really save you much.