Wednesday, December 3, 2008

Manifests: Reified Types

Reification (n.): When an abstract concept [...] is treated as a concrete 'thing'.[1]
When Java-the-language added generic types in version 1.5, Java-the-virtual-machine (JVM) did not. Generic types are a fiction of the compiler. They exist at compile time, but they are omitted from the generated bytecode and are therefore unavailable at run time. This phenomenon is known as erasure.

By contrast, when C# added generic types in version 2.0, the .NET Common Language Runtime (CLR) added them as well. This allows C# to access information about generic types at run time. We say that C#'s generic types are reified.

Java's type erasure leads to some peculiar limitations. To deal with these limitations, Java programmers often find themselves passing around java.lang.Class<T> objects: concrete manifestations of the otherwise abstract concept that is the type "T".

Starting with version 2.7.2, Scala has added manifests, an undocumented (and still experimental) feature for reifying types. They take advantage of a pre-existing Scala feature: implicit parameters.

To use Manifests, simply add an implicit scala.reflect.Manifest[T] parameter to your method, like so:
def name[T](implicit m: scala.reflect.Manifest[T]) = m.toString
This method prints the name of any Scala type. To use it, invoke the method with some type parameter:
name[Int => Int]
// returns "scala.Function1[int, int]"
When using implicit parameters, you usually have to declare a implicit identifier with the same type as your method expects, in the same scope where the method is called. With Manifests, the compiler automatically injects the implicit parameter for you, as long as it has enough type information to generate a Manifest. Essentially, this is a way of carrying over type information available at compile time into objects available at run time.

An Example: Generic Collections Optimized for Primitives

In Java, generic collections can't be used with primitive types. The boxed types must be used instead. This has implications for both run time performance and memory consumption. Despite HotSpot's best optimization efforts, boxed types can add significant overhead in performance-critical code.

In C#, by contrast, generic collections are optimized for performance with primitive types. This is possible because C#'s types are reified. If your code calls for a List<T>, C# knows at run time what T is and can use the specific implementation of List that is optimized for your T.

Let's see what Scala can do with Manifests and a lot of help from the fastutil library, which provides 1000+ (!) Java collection classes optimized for primitives.
import scala.reflect.Manifest
import it.unimi.dsi.fastutil._

def mkList[T](implicit m: Manifest[T]) = {
(m.toString match {
case "boolean" => new booleans.BooleanArrayList
case "byte" => new bytes.ByteArrayList
case "char" => new chars.CharArrayList
case "double" => new doubles.DoubleArrayList
case "float" => new floats.FloatArrayList
case "int" => new ints.IntArrayList
case "long" => new longs.LongArrayList
case "short" => new shorts.ShortArrayList
case _ => new objects.ObjectArrayList[T]
Now, invoking the mkList method with the proper type (i.e., mkList[Int], mkList[Double], etc.) will create an ArrayList optimized for that particular primitive type. We can verify that the correct classes are being instantiated by doing a little reflection.
// returns "it.unimi.dsi.fastutil.ints.IntArrayList"

// returns "it.unimi.dsi.fastutil.doubles.DoubleArrayList"
These optimized collections can have considerable benefits over their unoptimized counterparts in java.util. Just imagine, for example, the memory overhead of storing boxed Bytes instead of unboxed bytes. Storing just the pointer for one boxed Byte takes up at least four (and possibly eight) bytes!

Of course, the same effect could be achieved in Java by passing an explicit java.lang.Class<T> parameter to mkList. The disadvantage is that this imposes a significant overhead on the programmer to create and pass around these Class<T> objects. Scala's support for implicit parameters, and the automatic injection of Manifests by the compiler, makes reifying types on the JVM slightly less painful.

Appendix: Other Methods on Manifests

In this example, the only method we used on Manifests was toString. What other methods are available? Manifests are as-yet undocumented (not in the official Scaladocs), but looking through the source can give us an idea of what's available.


This method returns the java.lang.Class that corresponds to the run time erasure of the Scala type represented by the Manifest.

<:< and >:>

These methods test whether the type represented by the Manifest is a subtype or a supertype (respectively) of the type represented by the given parameter. As the source warns, the current implementation is merely an approximation, as it is based on the erasure of the two types.


This method tests whether two types are equal. Again, the source warns that the current implementation is merely an approximation, as it is based on the erasure of the two types.

Thanks to David Hall for suggesting the example.


Ismael Juma said...

Hi Jorge,

Just be careful not to use anonymous functions to work with the primitive collections or the results might not be what you expect.

In terms of generic specialisations, the following links are interesting:


Jorge Ortiz said...

Hi Ismael,

Yup, there are lots of pitfalls on the JVM. Unfortunately this is another area where C# "got it right".

As you explore in your blog post, this can be remedied with some compiler hacking. It'd be interesting to see an open source compiler plugin that produced specialized generic functions for use with primitive collections.

And thanks for the links! I wasn't aware of those efforts, and they certainly are interesting.

Ismael Juma said...

Hi Jorge,

Yes, I agree that .NET got it right in that respect.

I would also like to see a compiler plugin for that. In fact, I wanted to work on one, but time (or lack thereof) is the issue as usual.

Yes, I am quite interested to see how those efforts turn out.

By the way, two related things that are interesting are the -optimise flag for scalac and scalar replacement for HotSpot. I intend to do some tests with the former, and I've done some with the latter:

Richard said...

Actually, you can obtain compile time information at runtime with Java, overcoming type erasure.

This is possible if you employ anonymous classes. It's a dirty trick, in fact, but it can be done. See my article about this:

So, once ...

1. it is possible to keep type information at runtime (as I explained in my article)

2. scalac is a compiler which can potentially "implement it right", creating all data structures needed at runtime when RTTI is needed (and eventually not needing at all any trick like I explained), question is:

Why scalac is not able to produce multiple "incarnations" of a class, pretty much what C++ templates do?


Richard Gomes

Jorge Ortiz said...


That's a very neat trick! I'm impressed. The downsides are that you can't retrofit others' generic classes with type parameters, there's no way to guarantee such a class is instantiated in such a reified manner, and you can't propagate a requirement that a generic type be reified up the call stack.

To answer your question though: Scala can compile multiple specialized implementations of a class. Look at Scala's @specialized annotation.


alia said...

your example is great, it helped more than anything elsebuy research papers

Matthew Burdick said...

Your article is incredibly interesting! Browsing your work has educated me. Learned a lot from it. I will store your site and will pursue to read your future blog posts. Excellent! Kudos! Matthew, essay writing trainer

Mari said...

Mari said...

look.....essay writing service
Great post! Interesting information and cute writing style. essay writing service
but essay writing service I dont know why I always visit here

Елена Смит said...

I really likeyour blog. There are many interesting posts.

steve7876 said...

Your article has helped me to read this subject on a different level. I would like to thank your efforts for exploring this issue.Healthy body INC

get rid of cellulite said...

Like your writing! Still you can do some things to improve it.

M RAZA ABBAS said...

Thanks for a very interesting blog. What else can I write that kind of information in such a perfect approach to get? I have a company that I'm just now working on, and I look for more of these.

get rid of cellulite said...

I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity is so fantastic and working style so speedy. Thanks

steve7876 said...

The write-up features helped everyone to learn that issue using a different levels. I'd really like in order to give thanks to your energy pertaining to checking out this issue.Best Mobile wallet

Dewi Nova said...

information about the health of the area femininity. We introduce crystal x useful for women to overcome the problem of femininity. More intormation here Crystal X
Crystal X Jogja
Crystal x Nasa
Pusat Crystal X Jogja
Ciri-ciri Crystal X Asli
Pusat Crystal X Tegal

Alex Edward said...

I am quite inspired from your blogging skills, thanks for sharing it1
Watch Dogs Jacket

Ken Michale said...

This is one of the best blogs I have ever read. I m absolutely excited to get to read such a well blog. Friv | Kizi 7 | Yepi

Mark Taylor said...

It was dependably so fascinating to going by your site. What an extraordinary data, thank you for offering. This will help me such a great amount in my learning.

Visit: Earn Online High School Diploma

get rid of cellulite said...

Hello I am so delighted I found your blog, I really found you by mistake, while I was looking on Yahoo for something else, anyways I am here now and would just like to say thanks for a tremendous post. Please do keep up the great work.
Mindfulness Courses Dublin

rita espinosa said...

Well it's java, but I think on June 30 java and other internet platforms will crash because the our earth's rotation will slow down and we will have a leap second. how to get twitter re tweets

sabir said...

click here to get free brave frontier hack and cheats from the website that can provide you free gems and coins.

steve7876 said... are the providing the best coaches in Sydney and if you are looking for free classes then call us now at 0431 571 161

steve7876 said... are the providing the best coaches in Sydney and if you are looking for free classes then call us now at 0431 571 161

Raja Hindustani said...

hey gamers you are about to get the cheats for boom beach

Poplu Raj said...

If you want to know that contact support than click here and get all about free blitz brigade hack absolutely free.

Mark Taylor said...

You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me.

Visit : GED Online

Rock Star said...

Hey dear,your post awesome and fabulous article to get club penguin membership generator for free of cost than click here.

Lan Linh said...

Great information I will tweet to my friends to get them to check it out. keep it up. Thanks for sharing!
friv4 | friv 4 school | kizi4 | kizi 4|unblocked games| unblockedgames

steve7876 said...

The learning lab is providing the best Early Childhood Music Tutor Sydney and English tutor in Sydney and if you are looking for maths tutor for your child contact us now!

Michael Babbage said...

Nice Post, Thanks for your very useful information... I will bookmark for next reference.

steve7876 said...

I was looking for something like this ,Thank you for posting the great content……I found it quiet interesting, hopefully you will keep posting such blogs…

Cullen Ghormley said...

Really experienced great improvements in computer technology. Games like the vibrant, does not want to get people seated per PC. for fun

victor cate said...

It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot!
amazon gift cards hack

Rohit Kumar said...

This article is really fantastic and thanks for sharing the valuable post.

Aaron Lawrence said...

Such useful information is very nice. A very useful thing for people to publish value.


Peter Dicosta said...

Get your free codes of xbox live game here on this site and make your payable game absolutely free Thank you xbox live code gratuit

Peter Dicosta said...

Nice post i like this post this is an awesome post for xbox user free xbox live code generator

Aman Yadav said...

With sufficient time as well as processing power, it is a mathematical certainty that brute force attempts will certainly work.
hack facebook password online

Kiên Huệ said...

did not talk much as before
facebook entrar a mi perfil , facebook entrar a mi cuenta , facebook login , facebook entrar , facebook entrar , facebook entrar

Emily Trần said...

This story online correspondence will print a book that him descargar whatsapp fast , use app baixar whatsapp very comfortable , play mobogenie games hot descargar whatsapp gratis , baixar whatsapp gratis , descargar mobogenie gratis

Akash Singh said...

Thanks for sharing. I hope it will be helpful for too many people that are searching for this topic.
Movers and Packers in Pune

Akash Singh said...

Thanks for sharing. I hope it will be helpful for too many people that are searching for this topic.
Movers and Packers Hyderabad
Movers and Packers Bangalore
Movers and Packers Pune
Packers and Movers India

Akash Singh said...

Thanks for sharing. I hope it will be helpful for too many people that are searching for this topic.
Movers and Packers Delhi
Movers and Packers Gurgaon
Movers and Packers Mumbai

Akash Singh said...

Thanks for sharing. I hope it will be helpful for too many people that are searching for this topic.
Movers and Packers Pune

amazon said...

You have some really great posts and I feel I would be a good amazon gift card codes

Krish Dee said...

the verdict is that hacking facebook is an actual big deal, not everyone's breeze, I admit even I'm no place close to the degree of experience where I would certainly be playing with data sources on facebook web servers.

how to hack facebook account

Ramana narayan said...

Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign. packers and movers bangalore packers and movers marathahalli packers and movers hsr layout packers and movers btm layout packers and movers bommanahalli packers and movers whitefield packers and movers koramangala packers and movers jp nagar

Michael Twin said...

I really liked this part of the article. with a nice and interesting topics have helped a lot of people who do not challenge things people should know... you need more publicize this so many people who know about it are rare for people to know this. Success for you............. ...... ..... ...

Michael Amat said...

That a good' article, i usually amazed with' this thing, i asked myself about this opinion, I wish You'll a better articles that can make another people impressive..don't make the article feel rigit and isn't interesting and poor, i like to read this' article and i think this is "good".thank you' m'y brother..........

Alexandra Kate said...

Exceilent blog you have here but I was curious abou t if you knew of any communi ty forums tha t cover the same topics talked about in this article? I’d really like to be a part of online community where I can get advice from other experienced individuaIs that share the same interest. If you have any suggestions, please let me know. Appreciate it.....

Divya Bharti said...

Movers and Packers in Bangalore @

Packers and Movers Pune @

Packers and Movers Gurgaon @
Packers and Movers Hyderabad @

Packers and Movers in Mumbai @

Movers and Packers Delhi @

Michael Husen said...

I was about to say something on this topic. But now i can see that everything on this topic is very amazing and mind blowing, so i have nothing to say here. I am just going through all the topics and being appreciated. Thanks for sharing.

Shahid Khan said...

Thanks for this nice post indeed....
unblocked games
happy wheels game
Thanks Again !

Shahid Khan said...

US Open 2015, US Open 2015 Live, US Open 2015 live match, US Open 2015 live scores, US Open 2015 Live Stream, US Open 2015 Live Streaming, US Open Live, US Open Live results, US Open Live Stream, US Open live streaming, US Open streaming free, Watch US Open 2015 Live

amine lahragui said...

Your work is very good and I appreciate you and hopping for some more informative posts. happy new year 2016 new year 2016
bonne année 2016 imagenes de año nuevo 2016 imagenes de navidad 2016 feliz año nuevo 2016 Journée internationale des droits des femmes 2016

ams boss said...

Good post. I learn some thing tougher on distinct blogs everyday.
home warrantys
top 10 home warranty companies
home appliance warranty companies

Bella Tran said...

Thanks for sharing this with us loved reading your article
descargar mobogenie and apply baixar facebook and apply baixar mobogenie online free and apply descargar facebook and apply facebook descargar and descargar mobogenie and whatsapp descargar and juegos kizi

Rick Guerio said...

box office collection of prem ratan dhan payo movie

Thanks for sharing this admin.
I hope it will be more helpful for others also.

PRDP Box Office Collection

box office collection of prem ratan dhan payo movie

prem ratan dhan payo movie prediction prdp

box office collection of prem ratan dhan payo movie

prem ratan dhan payo movie prediction prdp