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

  • SEARCH
  • Home
  • 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 6475795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:44:52+00:00 2026-05-25T06:44:52+00:00

Consider this code: class Foo[T : Manifest](val id: String = manifest[T].erasure.getName) I basically want

  • 0

Consider this code:

class Foo[T : Manifest](val id: String = manifest[T].erasure.getName)

I basically want to store an identifier in Foo, which is often just the class name.

Subclass which do not need a special identifier could then easily use the default value.

But this doesn’t even compile, the error message is:

error: No Manifest available for T.

Is there another approach which will work?

EDIT:

Why does this work if the manifest isn’t available until the primary constructor?

class Foo[T: Manifest](val name: String) { 
  def this() = this(manifest[T].erasure.getName)
}
  • 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-25T06:44:53+00:00Added an answer on May 25, 2026 at 6:44 am

    When the syntactic sugar is removed from that context bound, it gets rewritten as:

    class Foo[T]
      (val id: String = implicitly[Manifest[T]].erasure.getName)
      (implicit ev$1: Manifest[T]) = ...
    

    So the Manifest evidence simply isn’t available when determining the default value of id. I’d instead write something like this:

    class Foo[T : Manifest](id0: String = "") {
      val id = if (id0 != "") id0 else manifest[T].erasure.getName
    }
    

    In your second approach (which is a great solution, by the way!), expect a rewrite similar to:

    class Foo[T](val name: String)(implicit x$1: Manifest[T]) { 
      def this()(implicit ev$2: Manifest[T]) = this(manifest[T].erasure.getName)
    }
    

    So yes, the manifest is available before the call to manifest[T].erasure

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

Sidebar

Related Questions

Consider this code: class Foo { public void doIt(String... strs) { System.out.println(this is varargs);
Consider such code (this is just example not real code): class Foo(url : String)
Consider this code : class MyClass<T> { } class AnotherClass : MyClass<String> { }
Consider the following code: class Foo(var name: String = bar) Now i try to
Consider this code class Foo { private: Bar bar; //note: no reference public: Foo(Bar&
Consider this: class Foo{ void func1(){ /*func1 code*/ } void func2(){ /*func2 code*/ }
Consider this class: public class Foo { // Fields private string _bar; // Properties
Consider this code sample: public abstract class Parent { public int val; public Parent()
Consider the following code (C# 4.0): public class Foo : LambdaExpression { } This
Consider this code: namespace foo {} class A { class B { }; friend

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.