sealed
class
DefaultsTo[A, B] extends AnyRef
Instance Constructors
-
new
DefaultsTo()
Value Members
-
final
def
!=(arg0: AnyRef): Boolean
-
final
def
!=(arg0: Any): Boolean
-
final
def
##(): Int
-
final
def
==(arg0: AnyRef): Boolean
-
final
def
==(arg0: Any): Boolean
-
final
def
asInstanceOf[T0]: T0
-
def
clone(): AnyRef
-
final
def
eq(arg0: AnyRef): Boolean
-
def
equals(arg0: Any): Boolean
-
def
finalize(): Unit
-
final
def
getClass(): Class[_]
-
def
hashCode(): Int
-
final
def
isInstanceOf[T0]: Boolean
-
final
def
ne(arg0: AnyRef): Boolean
-
final
def
notify(): Unit
-
final
def
notifyAll(): Unit
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
-
def
toString(): String
-
final
def
wait(): Unit
-
final
def
wait(arg0: Long, arg1: Int): Unit
-
final
def
wait(arg0: Long): Unit
Inherited from AnyRef
Inherited from Any
Neat helper to obtain a default type should one not be given eg:
The signature of the
find
method ensures that it can only be called if the caller can supply an object of typeDefaultsTo[T, Document]
. Of course, the DefaultsTo.default andDefaultsTo.overrideDefault methods make it easy to create such an object for any type
T. Since these methods are implicit, the compiler automatically handles the business of calling one of them and passing the result into
find.
But how does the compiler know which method to call? It uses its type inference and implicit resolution rules to determine the appropriate method. There are three cases to consider:
1.
find
is called with no type parameter. In this case, type T must be inferred. Searching for an implicit method that can provide an object of typeDefaultsTo[T, Document]
, the compiler findsdefault
andoverrideDefault
.default
is chosen since it has priority (because it's defined in a proper subclass of the trait that defines overrideDefault). As a result, T must be bound toDocument.
2.
find
is called with a non-Document type parameter (e.g.,find[Document]()
). In this case, an object of typeDefaultsTo[Document]
must be supplied. Only theoverrideDefault
method can supply it, so the compiler inserts the appropriate call.3.
find
is called withDocument
as the type parameter. Again, either method is applicable, but default wins due to its higher priority.