- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
class X { type T } vs class X[T]
Tue, 2009-01-20, 22:45
Hi,
Could someone summarize what's the differences between declaring a type
parameter using:
class X[T] ...
and
class X {
type T
}
How does one differentiate between which to use?
Here is some code with the second way:
abstract class SubjectObserver {
type S <: Subject
type O <: Observer
abstract class Subject {
self: S =>
private var observers: List[O] = List()
def subscribe(obs: O) =
observers = obs :: observers
def publish =
for (val obs <- observers)
obs.notify(this)
}
trait Observer {
def notify(sub: S): Unit
}
}
I seem to be able to rewrite this as following. (This is as close as I got).
abstract class SubjectObserver[S <: Subject[S, O], O <: Observer[S]] {
}
abstract class Subject[S, O <: Observer[S]] {
self: S =>
private var observers: List[O] = List()
def subscribe(obs: O) = observers = obs :: observers
def publish = for (val obs <- observers) obs.notify(this)
}
trait Observer[S] {
def notify(sub: S): Unit
}
//Now SubjectObject is rendered pointless, so perhaps I should look
there for the benefits the other way has, which I do not see
And can use it like this:
object Main {
def main(args: Array[String]): Unit = {
val mySubj = new MySubject
val myObs = new MyObserver
mySubj.subscribe(myObs)
mySubj.publish
()
}
class MySubject extends Subject[MySubject, MyObserver]
class MyObserver extends Observer[MySubject] {
def notify(subj: MySubject) = println("notified")
}
}
Is this always possible? Can one use only one style? When would I choose
the other way? (Ok, so all these are different phrasings to ask the same
question, you get the point!)
(Or is any online ref that explains the difference?
Thanks a lot,
Dimitris
Wed, 2009-01-21, 00:37
#2
Re: class X { type T } vs class X[T]
That's great, thanks for the pointer! (It would be nice if this was
included in http://www.scala-lang.org/node/143#papers too)
Cheers,
Dimitris
O/H martin odersky έγραψε:
> On Tue, Jan 20, 2009 at 10:33 PM, Andreou Dimitris
> wrote:
>
>> Hi,
>>
>> Could someone summarize what's the differences between declaring a type
>> parameter using:
>>
>> class X[T] ...
>>
>> and
>>
>> class X {
>> type T
>> }
>>
>> How does one differentiate between which to use?
>>
>>
> Here's a paper about this:
>
> A Statically Safe Alternative to Virtual Types, Kim Bruce, Martin
> Odersky, and Philip Wadler. Proc. ECOOP'98.
>
> In short, abstract types are better if there are many mutually
> recursive type variables, whereas type parameters allow more
> lightweight instantiation.
>
> Cheers
>
On Tue, Jan 20, 2009 at 10:33 PM, Andreou Dimitris
wrote:
> Hi,
>
> Could someone summarize what's the differences between declaring a type
> parameter using:
>
> class X[T] ...
>
> and
>
> class X {
> type T
> }
>
> How does one differentiate between which to use?
>
Here's a paper about this:
A Statically Safe Alternative to Virtual Types, Kim Bruce, Martin
Odersky, and Philip Wadler. Proc. ECOOP'98.
In short, abstract types are better if there are many mutually
recursive type variables, whereas type parameters allow more
lightweight instantiation.
Cheers