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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:46:56+00:00 2026-05-16T11:46:56+00:00

I have some Scala code that makes fairly heavy use of generics, and I

  • 0

I have some Scala code that makes fairly heavy use of generics, and I have gleaned from the docs that using a manifest in the parametrization constraints can help me work around the type erasure issues (e.g. I want to instantiate a new object of the generic type). Only, I’d like to understand more about how this works. It almost feels like some sort of hashmap that’s getting an entry for every invocation site… Can anyone here elaborate?

class Image[T <: Pixel[T] : Manifest](fun() => T, size: Array[Int], data: Array[T]) {
    def this(fun: () => T, size: Array[T]) {
        this(fun, size, new Array[T](size(0) * size(1));
    }
}

This is something that doesn’t seem to be covered in any of the documentation that I found on the site, and on Google I mostly get older posts that have very different syntax, and since 2.8 seems to have a lot of things changed, I’m not sure those are still accurate.

  • 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-16T11:46:56+00:00Added an answer on May 16, 2026 at 11:46 am

    It’s been awhile since I dug through the source code for Scala in a quest to answer the same question… but the short answer, as I recall –

    Manifest is a cheat code to allow the COMPILER to get around Type erasure (it isn’t being used at runtime). It causes multiple code paths to be generated at compile time for the possible input types matching the manifest.

    The Manifest is resolved implicitly, but if there is any ambiguity at compile time about what the Manifest type is, the compiler WILL stop.

    With a copy of a Manifest you have a few things available. The main things you typically want is either the java.lang.Class that was erased via erasure:

    class BoundedManifest[T <: Any : Manifest](value: T) {
      val m = manifest[T]
      m.erasure.toString match {
        case "class java.lang.String" => println("String")
        case "double" | "int"  => println("Numeric value.")
        case x => println("WTF is a '%s'?".format(x))
        }
    }
    
    class ImplicitManifest[T <: Any](value: T)(implicit m: Manifest[T]) {
      m.erasure.toString match {
        case "class java.lang.String" => println("String")
        case "double" | "int" => println("Numeric value.")
        case x => println("WTF is a '%s'?".format(x))
      }
    }
    
    new BoundedManifest("Foo Bar!")
    // String 
    new BoundedManifest(5)
    // Numeric value.
    new BoundedManifest(5.2)
    // Numeric value.
    new BoundedManifest(BigDecimal("8.62234525"))
    // WTF is a 'class scala.math.BigDecimal'?
    new ImplicitManifest("Foo Bar!")
    // String 
    new ImplicitManifest(5)
    // Numeric value.
    new ImplicitManifest(5.2)
    // Numeric value.
    new ImplicitManifest(BigDecimal("8.62234525"))
    // WTF is a 'class scala.math.BigDecimal'?
    

    This is a rather wonky example but shows what is going on. I ran that for the output as well FWIW on Scala 2.8.

    The [T ... : Manifest] boundary is new in Scala 2.8… you used to have to grab the manifest implicitly as shown in ImplicitManifest. You don’t actually GET a copy of the Manifest. But you can fetch one inside your code by saying val m = manifest[T] … manifest[_] is defined on Predef and demonstrably will find the proper manifest type inside a boundaried block.

    The other two major items you get from a Manifest is <:< and >:> which test subtype/supertype of one manifest versus another. If I recall correctly these are VERY naive implementation wise and don’t always match but I have a bunch of production code using them to test against a few possible erased inputs. A simple example of checking against another manifest:

    class BoundedManifestCheck[T <: Any : Manifest](value: T) {
      val m = manifest[T]
      if (m <:< manifest[AnyVal]) {
        println("AnyVal (primitive)")
      } else if (m <:< manifest[AnyRef]) {
        println("AnyRef")
      } else {
        println("Not sure what the base type of manifest '%s' is.".format(m.erasure))
      }
    }
    
    
    new BoundedManifestCheck("Foo Bar!")
    // AnyRef
    new BoundedManifestCheck(5)
    // AnyVal (primitive)
    new BoundedManifestCheck(5.2)    
    // AnyVal (primitive)
    new BoundedManifestCheck(BigDecimal("8.62234525"))
    // AnyRef
    

    Jorge Ortiz has a great blog post (albeit old) on this: http://www.scala-blogs.org/2008/10/manifests-reified-types.html

    EDIT:

    You can actually see what Scala is doing by asking it to print out the results of the erasure compiler phase.

    Running, on my last example above scala -Xprint:erasure test.scala produces the following result:

    final class Main extends java.lang.Object with ScalaObject {
      def this(): object Main = {
        Main.super.this();
        ()
      };
      def main(argv: Array[java.lang.String]): Unit = {
        val args: Array[java.lang.String] = argv;
        {
          final class $anon extends java.lang.Object {
            def this(): anonymous class $anon = {
              $anon.super.this();
              ()
            };
            class BoundedManifestCheck extends java.lang.Object with ScalaObject {
              <paramaccessor> private[this] val value: java.lang.Object = _;
              implicit <paramaccessor> private[this] val evidence$1: scala.reflect.Manifest = _;
              def this($outer: anonymous class $anon, value: java.lang.Object, evidence$1: scala.reflect.Manifest): BoundedManifestCheck = {
                BoundedManifestCheck.super.this();
                ()
              };
              private[this] val m: scala.reflect.Manifest = scala.this.Predef.manifest(BoundedManifestCheck.this.evidence$1);
              <stable> <accessor> def m(): scala.reflect.Manifest = BoundedManifestCheck.this.m;
              if (BoundedManifestCheck.this.m().<:<(scala.this.Predef.manifest(reflect.this.Manifest.AnyVal())))
                scala.this.Predef.println("AnyVal (primitive)")
              else
                if (BoundedManifestCheck.this.m().<:<(scala.this.Predef.manifest(reflect.this.Manifest.Object())))
                  scala.this.Predef.println("AnyRef")
                else
                  scala.this.Predef.println(scala.this.Predef.augmentString("Not sure what the base type of manifest '%s' is.").format(scala.this.Predef.genericWrapArray(Array[java.lang.Object]{BoundedManifestCheck.this.m().erasure()})));
              protected <synthetic> <paramaccessor> val $outer: anonymous class $anon = _;
              <synthetic> <stable> def Main$$anon$BoundedManifestCheck$$$outer(): anonymous class $anon = BoundedManifestCheck.this.$outer
            };
            new BoundedManifestCheck($anon.this, "Foo Bar!", reflect.this.Manifest.classType(classOf[java.lang.String]));
            new BoundedManifestCheck($anon.this, scala.Int.box(5), reflect.this.Manifest.Int());
            new BoundedManifestCheck($anon.this, scala.Double.box(5.2), reflect.this.Manifest.Double());
            new BoundedManifestCheck($anon.this, scala.package.BigDecimal().apply("8.62234525"), reflect.this.Manifest.classType(classOf[scala.math.BigDecimal]))
          };
          {
            new anonymous class $anon();
            ()
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Scala application using Akka that receives REST requests, makes some operations
I have been trying to write some code in Scala to read a file
I have created a project that uses scala that has some abstract classes definitions
I have a Scala application that relies on an external RESTful webservice for some
I have a code in scala that, for various reasons, have few lines of
I have some Scala code roughly analogous to this: object Foo { val thingA
I have some Scala code I cannot get to compile, and I simplified it
I'm trying to test-drive some Scala code using Specs2 and Mockito. I'm relatively new
I'm converting some old Java code to Scala. I have this. class XYCustomRenderer(dataMax: Double,
If I have some erroneous code, commented in a *scala.html template file in Play,

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.