Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6117529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:19:52+00:00 2026-05-23T15:19:52+00:00

//API class Node class Person extends Node object Finder { def find[T <: Node](name:

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:19:52+00:00Added an answer on May 23, 2026 at 3:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I'm using the Users API incorrectly: class BaseHandler(webapp.RequestHandler): user = users.get_current_user() def
I'm using the Facebook API and the class produces this object. object(User)#4 (5) {
I want to provide something like this in my api: class Foobar extends AbstractThing<Double>
I'm trying to serialize an object that have a duplicate node name using C#,
I'm using a Google Analytics API Class in PHP made by Doug Tan to
According to the class ElementType in the API, you can annotate local variables and
I am writing an Objective-C class but it uses an API written in C.
I am trying to place API variables on ONE class file or mxml file
I'm having an issue using the Cookie class of the Servlet API 2.5 on
Recently, I've released an open-source class - wrapper of API functions available from one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.