One of the things holding scala back from being a more robust language is the fact that it runs on the JVM. (On the other hand, this is also one of its strengths–you can easily interoperate with existing Java code and libraries, as well as any other code that runs ont he JVM, like Groovy or Clojure.) Because Scala runs ont he JVM, it suffers from type erasure, which means that any generic type parameters that are in the code are lost after compilation, and are no longer present in the byte code, and thus at runtime. (This was a design decision made by the Java team when they introduced generics in Java 1.5, in order to preserve byte code backward-compatibility.)
In order to work around this, Scala introduced the Manifest class, which captures the type information during compilation, and allows you to access this type at runtime. This was originally designed to allow for the creation of arrays of the generic type at runtime, but it can also be used for other custom generic types. It uses implicit parameters to capture this information.
The problem (that I had), though is that you can’t seem to access a class’s type parameters directly in a method of that class. So, this does’t work:
import scala.reflect.Manifest
class MyClass[T: Manifest] {
def myType(implicit m: scala.reflect.Manifest[T]) = m.toString
}
You won’t get a compilation error, but you will just get ‘Nothing’ as your type inside your method:
scala> new MyClass[Long] res17: MyClass[Long] = MyClass@2769aba2 scala> res15.myType res18: java.lang.String = Nothing
This is because there was no type parameter to the method itself, and the [T] in the method’s implicit argument is not the same [T] that is used to parameterize the class. If we add a type parameter to the ‘myType’ method, we can get closer to what we want:
class MyClass2[T: Manifest] {
def myType[U](implicit m: scala.reflect.Manifest[U]) = m.toString
}
Then, we can try it out:
scala> new MyClass2[Long] res19: MyClass2[Long] = MyClass2@6c6455ae scala> res19.myType[Int] res20: java.lang.String = Int
Okay, so now we are getting some type information out, but it really isn’t ideal. Note that the type parameter inside the ‘myType’ method was ‘Int’, which was the parameter we passed to the method, not ‘Long’, which is what we passed to the class. What if we want to get the class’s type parameter? We certainly don’t want the calling code to have to keep track of the type parameters in multiple places (once at the constructor call, and once more at the method invocation). So, we can add a second method to help smooth this out:
class MyClass3[T: Manifest] {
def myType[U](implicit m: scala.reflect.Manifest[U]) = m.toString
def myTypeWtihoutExtraParam = myType[T]
}
And, then, we get:
scala> new MyClass3[Long] res30: MyClass3[Long] = MyClass3@543d8ee8 scala> res30.myTypeWtihoutExtraParam res31: java.lang.String = Long
So, you can see that the type that was passed in to the class constructor as a generic parameter is now available in the method, without the method caller needing to supply it after the object is created.
A couple of things to note:
- Don’t forget to import scala.reflect.Manifest
- The type parameter in the constructor definition is context-scoped, so the ‘: Manifest’ part is important. This technique won’t work if you just specify the parameter as ‘[T]‘. If you leave this off, you will get an error that reads, “No Manifest available for T.”
I’ve been active on the programming question & answer site, StackOverflow.com for a few years now, and in the past several months, the company behind that wonderful resource has branched out into other topics. This week, the new 






LivingSocial/Fiesta Americana Resort Bait & Switch Scam
Under one of the bullets in the “escape kit” it states: “Daily Continental Breakfast, Afternoon Cocktails, and Hors D’œuvres at the Grand Club and Free Upgrade to Grand Club Level for First 50 Vouchers Sold”. This gives the impression that daily continental breakfast, afternoon cocktails and hors D’oeuvres are included in the package deal and that the first 50 vouchers sold will receive free upgrade to Grand Club Level. After being denied the continental breakfast, we spoke to the front desk. The front desk people read it as “all of the items in this statement are only for the first 50 people. This is a very tricky and misleading statement. Furthermore, in the body of the webpage, it states
This states nothing about the “first 50 vochers sold”. We probably would not have bought the package had we know that the continental breakfast was not included, and instead found a package that included the amenities we were looking for at the right price.
Under another bullet it states: “In-Room Bottle of Wine and Canapés Daily”. When we first arrived, there was a bottle of wine and a plate of canapés in our room waiting for us. The reminder of the days, there was no wine or canapés. When we asked the front desk about this, they told us that a bottle of wine was only supplied on the first day and that Living Social lied about that in their ad. One front desk woman even admitted that the statement was a bait and switch tactic.
Overall, we are very unimpressed with Living Social’s misleading tactics. We would love to be able to take advantage of the wonderful offers they have available, however they have lost a lot of our trust.