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 9230311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:47:40+00:00 2026-06-18T05:47:40+00:00

I have a DSL and some runtime code. The problem is I got somewhere

  • 0

I have a DSL and some runtime code. The problem is I got somewhere at runtime:

val clazz: Class[_ <: java.io.Serializable] = classOf[java.lang.String]
val value: java.io.Serializable = "abc"

and I have a class

class MyWrapper[T <: java.io.Serializable](val data: T)(implicit m: Manifest[T]) {
  override def toString = m.runtimeClass
}
val wrapper = new MyWrapper(value)

The problem is I need to return java.lang.String from a call of toString. But I got java.io.Serializable. Unfortunately I am neither able to create fixed pattern matching for each java.io.Serializable subtype (this would be crazy) nor to create MyWrapper explicit with new MyWrapper[String](value). I don’t know the type of value, maybe it is a subtype of Serializable.

Is there some way to pass type/manifest value at runtime if I know that value type is equal to variable clazz?


Update (solution from Régis Jean-Gilles don’t work). REPL test:

val clazz: Class[_ <: java.io.Serializable] = classOf[java.lang.String]
val value: java.io.Serializable = "abc"

class MyWrapper[T <: java.io.Serializable](val data: T)(implicit m: Manifest[T]) {
  override def toString = m.runtimeClass.getName
}
val wrapper = new MyWrapper(value)
//val wrapper = new MyWrapper(value)(ClassManifest.fromClass(clazz).asInstanceOf[ClassTypeManifest[java.io.Serializable]])
//val wrapper = new MyWrapper(value)(ClassManifest.fromClass(clazz))
System.err.println(wrapper.toString)

I got an error, if I tried to pass manifest explicit:

scala> :load test.scala
Loading test.scala...
clazz: Class[_ <: java.io.Serializable] = class java.lang.String
value: java.io.Serializable = abc
defined class MyWrapper
<console>:10: error: type mismatch;
 found   : scala.reflect.ClassManifest[_$1] where type _$1 <: java.io.Serializable
    (which expands to)  scala.reflect.ClassTag[_$1]
 required: Manifest[java.io.Serializable]
       val wrapper = new MyWrapper(value)(ClassManifest.fromClass(clazz))
                                                                 ^
<console>:8: error: not found: value wrapper
              System.err.println(wrapper.toString)

Also I am unable to cast manifest explicit.

There is more strange error when I try to compile my application –

[error]  found   : scala.reflect.ClassManifest[(some other)_0(in method setProperty)]
[error]     (which expands to)  scala.reflect.ClassTag[(some other)_0(in method setProperty)]
[error]  required: Manifest[_0(in method setProperty)]
[error]           new Value.Static(default, Context.virtual(element))(ClassManifest.fromClass(elementProperty.typeClass)))

IMHO Régis Jean-Gilles very very close to solution. How to make it work with Scala 2.10?

  • 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-06-18T05:47:42+00:00Added an answer on June 18, 2026 at 5:47 am

    If I understand correctly, you are using manifests to work around type erasure but at a specific point all you have is a Class[_] so you need to convert it back to a manifest. Correct?

    If so, you can use ClassManifest.fromClass, and then explicitly pass it as the implicit value m.

    val wrapper = new MyWrapper(value)(Manifest.classType(clazz))
    

    You should probably abstract it away:

    object MyWrapper {
      def fromRuntimeType[T <: java.io.Serializable]( value: T ): MyWrapper[T] = {
        new MyWrapper(value)(Manifest.classType(value.getClass))
      }
    }
    

    Keep in mind though that because you only have a Class instance in the first place, you are at the mercy of type erasure again. This means that if the class (that you need to get a manifest for) is generic, you won’t have any type parameter information in the manifest returned by ClassManifest.fromClass.

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

Sidebar

Related Questions

I've got some code originating from a DSL that needs to be translated for
I have a DSL - like piece of Ruby code that looks like this:
I have a DSL Java object, i.e. a POJO which returns this in setters
I have some code with nested calls to flatMap like so: foo.flatMap(implicit f =>
I have the following code public class BaseController : Controller { SqlConnection con; DataSet
I have a DSL with Java front-end and I would like to serialize an
I have file with dsl-like stuff data. There I want to declore some s-expression
I am migrating some code from Scala 2.10-M5 to Scala 2.10-M7 . I have
I have a class that contains some private attributes. What I would like to
So I have some code for running a batch file as a specific user.

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.