This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Abstract Types vs. Generics

4 replies
David Copeland
Joined: 2009-06-16,
User offline. Last seen 42 years 45 weeks ago.
Other than using abstract types as an alias mechanism, when would you use them over generics?

class Foo[T] {
}

vs.

class Foo {
  type T
}


Matt Hellige
Joined: 2009-01-28,
User offline. Last seen 42 years 45 weeks ago.
Re: Abstract Types vs. Generics

On Sun, Aug 16, 2009 at 2:18 PM, David Copeland wrote:
> Other than using abstract types as an alias mechanism, when would you use
> them over generics?
>

Consider using an abstract type when most of the client code doesn't
care what the parameter is. In other words, if most of the sites look
like:
blah[T](foo: Foo[T])
leaving the variable unbound and immediately exposing it to the
caller, it might be simpler to make T an abstract type.

Matt

Rob Dickens
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Abstract Types vs. Generics
For one example, have a read of the paper referenced in the 'Scala API for remote monitoring and control' news item on thehttp://scala-lang.org front page.
2009/8/16 David Copeland <davec [at] naildrivin5 [dot] com>
Other than using abstract types as an alias mechanism, when would you use them over generics?

class Foo[T] {
}

vs.

class Foo {
  type T
}





--
Rob, Lafros.com
Rob Dickens
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Abstract Types vs. Generics
Basically, you have to use a type member (as opposed to parameter) where you want the type to be constrained by an inner class/trait.

2009/8/17 Rob Dickens <arctic [dot] bob [at] googlemail [dot] com>
For one example, have a read of the paper referenced in the 'Scala API for remote monitoring and control' news item on the http://scala-lang.org front page.
2009/8/16 David Copeland <davec [at] naildrivin5 [dot] com>
Other than using abstract types as an alias mechanism, when would you use them over generics?

class Foo[T] {
}

vs.

class Foo {
  type T
}





--
Rob, Lafros.com



--
Rob, Lafros.com
davetron5000
Joined: 2009-06-07,
User offline. Last seen 2 years 31 weeks ago.
Re: Abstract Types vs. Generics
Had I asked this on stackoverflow (maybe I should next time?), this would be the answer I would've marked as "accepted".

I'd love a concrete example(*), but I think I could come up with one on my own.

Dave

(*) for me, a concrete example must be in a concrete/non-abstract domain (which, sadly, mathematics is not :)

On Sun, Aug 16, 2009 at 4:38 PM, Matt Hellige <matt [at] immute [dot] net> wrote:
On Sun, Aug 16, 2009 at 2:18 PM, David Copeland<davec [at] naildrivin5 [dot] com> wrote:
> Other than using abstract types as an alias mechanism, when would you use
> them over generics?
>

Consider using an abstract type when most of the client code doesn't
care what the parameter is. In other words, if most of the sites look
like:
 blah[T](foo: Foo[T])
leaving the variable unbound and immediately exposing it to the
caller, it might be simpler to make T an abstract type.

Matt

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland