Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there example of scala abstract type usage which is impossible to achieve with generics?

There are two possible way to express abstraction over types.

abstract class Buffer {
  type T
  val element: T
}

rather that generics, e.g.

abstract class Buffer[T] {
  val element: T
}

I understand benefits in usability of using different approaches in different contexts. But I'm interest in examples where it is absolutely impossible to convert Abstract Type version to Generic version.

PS Code snippets are welcome.

like image 769
yura Avatar asked Sep 30 '11 21:09

yura


1 Answers

Abstract types can be bound to path dependent types which is not possible with type parameters. Thus you can e.g. implement an abstract type with a concrete inner class:

trait A { type T }
class B extends A { class T }

or explicitly bind it to a path dependent type inside the classes scope:

class C { type T = this.type }
class D { object Q; type T = Q.type }
like image 150
Moritz Avatar answered Nov 15 '22 19:11

Moritz