Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

generics - Is it possible in Scala to force the caller to specify a type parameter for a polymorphic method?

//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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yet another solution is to specify a default type for the parameter as follows:

object Finder {
   def find[T <: Node](name: String)(implicit e: T DefaultsTo Node): T = 
      doFind(name).asInstanceOf[T]
}

The key is to define the following phantom type to act as a witness for the default:

sealed class DefaultsTo[A, B]
trait LowPriorityDefaultsTo {
   implicit def overrideDefault[A,B] = new DefaultsTo[A,B]
}
object DefaultsTo extends LowPriorityDefaultsTo {
   implicit def default[B] = new DefaultsTo[B, B]
}

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 find method ensures that it can only be called if the caller can supply an object of type DefaultsTo[T, Node]. Of course, the default and 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 type DefaultsTo[T, Node], the compiler finds default and overrideDefault. 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 to Node.

  2. find is called with a non-Node type parameter (e.g., find[MyObj]("name")). In this case, an object of type DefaultsTo[MyObj, Node] must be supplied. Only the overrideDefault method can supply it, so the compiler inserts the appropriate call.

  3. find is called with Node as the type parameter. Again, either method is applicable, but default wins due to its higher priority.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...