org.mongodb.scala.bson.DefaultHelper
(Since version ) see corresponding Javadoc for more information.
Neat helper to obtain a default type should one not be given eg:
The signature of the
findmethod 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 typeT. Since these methods are implicit, the compiler automatically handles the business of calling one of them and passing the result intofind.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.
findis 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 findsdefaultandoverrideDefault.defaultis 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.
findis called with a non-Document type parameter (e.g.,find[BsonDocument]()). In this case, an object of typeDefaultsTo[BsonDocument, Document]must be supplied. Only theoverrideDefaultmethod can supply it, so the compiler inserts the appropriate call.3.
findis called withDocumentas the type parameter. Again, either method is applicable, but default wins due to its higher priority.