Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 747
Alex Kravets Avatar asked Dec 09 '10 22:12

Alex Kravets


People also ask

How does Scala determine types when they are not specified?

Non-value types capture properties of identifiers that are not values. For example, a type constructor does not directly specify a type of values. However, when a type constructor is applied to the correct type arguments, it yields a first-order type, which may be a value type.

What do you call Scala method that is parameterized by type as well as by value?

Language. Methods in Scala can be parameterized by type as well as value. The syntax is similar to that of generic classes. Type parameters are enclosed in square brackets, while value parameters are enclosed in parentheses.

What is generic function in Scala?

In Scala, forming a Generic Class is extremely analogous to the forming of generic classes in Java. The classes that takes a type just like a parameter are known to be Generic Classes in Scala. This classes takes a type like a parameter inside the square brackets i.e, [ ].

What are types in Scala?

Scala Type Hierarchy AnyVal represents value types. There are nine predefined value types and they are non-nullable: Double , Float , Long , Int , Short , Byte , Char , Unit , and Boolean . Unit is a value type which carries no meaningful information.


1 Answers

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.

like image 137
Aaron Novstrup Avatar answered Sep 24 '22 21:09

Aaron Novstrup