4

Possible Duplicate:
Scala: Abstract Types vs Generics

The way I understand it, the following two class definitions are the same. So what is the difference, other than the syntax?

abstract class Container[T] {}
class IntContainer extends Container[Int] {}

abstract class Container2 { type T }
class IntContainer2 extends Container2 { type T = Int }

When I look at the decompiled byte code, I notice that the first set still have generics (although IntContainer is defined as having type Object: public class IntContainer extends Container<Object> ). The second set has no such information. I thought all generic types were erased anyway...

PS Using Scala 2.10-M5

Community
  • 1
  • 1
John Smith
  • 2,797
  • 5
  • 27
  • 30
  • Is this as far as you've tried, or have you tried to actually use these `IntContainer` objects? – Gabe Aug 04 '12 at 13:24
  • I added an ArrayBuffer field to each superclass, and added an "add" method to each superclass, which delegates to the Buffer, and in both cases it works as expected. Attempting to add a String fails in both cases, with "type mismatch; found : String("asdf") required: Asdf.ic2.T (which expands to) Int". What was your point @Gabe, because I think I missed it? – John Smith Aug 04 '12 at 13:51
  • 4
    See e.g. [this](http://stackoverflow.com/q/1154571/334519) or [this](http://stackoverflow.com/q/3170821/334519) or [this](http://stackoverflow.com/q/2066835/334519) or [this](http://daily-scala.blogspot.com/2010/05/abstract-types-vs-parameter.html) or [this](http://www.artima.com/weblogs/viewpost.jsp?thread=270195). – Travis Brown Aug 04 '12 at 13:53
  • @TravisBrown The first, fourth and fifth links are the best, highlighting the advantages / disadvantages of each. Thanks! – John Smith Aug 04 '12 at 14:57

1 Answers1

2

There is a clear difference among abstract types and generics which we always need to remember:

  • If a class as a generic type, multiple instances of the same class could have different generic type
  • If a concrete class as an abstract type, for a given Scala class, all the instances of that (exactly that) class will have the same abstract type.

It should now be clear to you that the two can be used in a different way:

  • Generics are mainly designed for class who have to act as containers, or business classes who should perform some operations on a given target. In that sense, the generic parameter allow you to check type safety at compile time
  • Abstract types are mainly designed to define a behaviour or a property at higher level and to refine it when subclassing. Generics have little to do with inheritance

If you look to another perspective, while it is a common feature to pass as a parameter to a method a GenericClass[T] and a T, it is much more rare to design a method which receives a ClassWithAbstractTypeT and a classWithAbstractType.T . In fact, coding a method like that in Scala 2.9 is forbidden unless path-dependent mehods are active (if I am not wrong the are active by default in 2.10)

Edmondo1984
  • 17,841
  • 12
  • 55
  • 99
  • Interestingly, Martin Odersky claims that abtract types can do everything generics can, and even suggests removing generics from a later version of the language (possibly in the 4.0 timeframe). – Jörg W Mittag Aug 04 '12 at 16:44
  • Hmm. Removing generics altogether? Could you provide more details? – Eugene Burmako Aug 04 '12 at 20:42
  • Martin Odersky suggests it here, although doesn't mention a version 4.x: https://groups.google.com/forum/?hl=en&fromgroups#!msg/scala-language/PV4q6O1qIh8/yG4p8PA2Jf8J – John Smith Aug 04 '12 at 22:19