Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use existential type in Scala?

Tags:

scala

Give the following to methods:

def beCool[T <: S](args:Array[T]) = {}
def beCool(args:Array[T forSome {type T <:S}]) = {}

Are they equivalent? Can you give me some examples when should prefer which?

like image 377
Sawyer Avatar asked Mar 21 '11 06:03

Sawyer


1 Answers

You would need the first one, i think, whenever you need access to T. The simplest example is returning an element of args:

def beCool(args: Array[T forSome { type T }]): T = args.head // --> not found: type T
def beCool[T](args: Array[T]): T = args.head // ok

the inexistance of an accessible type T in the first one is more apparent when you use the wildcard:

def beCool(args: Array[_ <: S]) = ???
like image 191
0__ Avatar answered Oct 01 '22 01:10

0__