//API
class Node
class Person extends Node
object Finder
{
def find[T <: Node](name: String): T = doFind(name).asInstanceOf[T]
}
//Call site (correct)
val person = find[Person]("joe")
//Call site (dies with a ClassCast inside b/c inferred type is Nothing)
val person = find("joe")
In the code above the client site “forgot” to specify the type parameter, as the API writer I want that to mean “just return Node”. Is there any way to define a generic method (not a class) to achieve this (or equivalent). Note: using a manifest inside the implementation to do the cast if (manifest != scala.reflect.Manifest.Nothing) won’t compile … I have a nagging feeling that some Scala Wizard knows how to use Predef.<:< for this 🙂
Ideas ?
Yet another solution is to specify a default type for the parameter as follows:
The key is to define the following phantom type to act as a witness for the default:
The advantage of this approach is that it avoids the error altogether (at both run-time and compile-time). If the caller does not specify the type parameter, it defaults to
Node.Explanation:
The signature of the
findmethod ensures that it can only be called if the caller can supply an object of typeDefaultsTo[T, Node]. Of course, thedefaultandoverrideDefaultmethods 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:
findis called with no type parameter. In this case, typeTmust be inferred. Searching for an implicit method that can provide an object of typeDefaultsTo[T, Node], the compiler findsdefaultandoverrideDefault.defaultis chosen since it has priority (because it’s defined in a proper subclass of the trait that definesoverrideDefault). As a result,Tmust be bound toNode.findis called with a non-Nodetype parameter (e.g.,find[MyObj]("name")). In this case, an object of typeDefaultsTo[MyObj, Node]must be supplied. Only theoverrideDefaultmethod can supply it, so the compiler inserts the appropriate call.findis called withNodeas the type parameter. Again, either method is applicable, butdefaultwins due to its higher priority.