views:

128

answers:

5

Is it possible to do something like this in Scala:

class MyTest {
   def foo[A <: String _or_ A <: Int](p:List[A]) =  {} 
}

That is, the type A could be a String or Int. Is this possible?

(Similar question here)

+4  A: 

Not really possible as you put it, but you can do it using the type class pattern. For example, from here:

sealed abstract class Acceptable[T]
object Acceptable {
  implicit object IntOk extends Acceptable[Int]
  implicit object LongOk extends Acceptable[Long]
}

def f[T: Acceptable](t: T) = t

scala> f(1)
res0: Int = 1

scala> f(1L)
res1: Long = 1

scala> f(1.0)
<console>:8: error: could not find implicit value for parameter ev: Acceptable[Double]
f(1.0)
^

EDIT

This works if class and object are companions. On REPL, if you type each on a different line (ie, a "result" appears between them), they are not companions. You can type it like below, though:

scala> sealed abstract class Acceptable[T]; object Acceptable {
     |   implicit object IntOk extends Acceptable[Int]
     |   implicit object LongOk extends Acceptable[Long]
     | }
defined class Acceptable
defined module Acceptable
Daniel
Thanks, also reference http://ropas.snu.ac.kr/~bruno/papers/TypeClasses.pdf
oluies
@Brent As I said by e-mail, this is probably a result of typing the class an object on different lines. See my edit.
Daniel
+1  A: 

There is this <em>hack</em>:

implicit val x: Int = 0
def foo(a: List[Int])(implicit ignore: Int) { }

implicit val y = ""
def foo(a: List[String])(implicit ignore: String) { }

foo(1::2::Nil)
foo("a"::"b"::Nil)

See http://michid.wordpress.com/2010/06/14/working-around-type-erasure-ambiguities-scala/

And also this question.

michid
+4  A: 

You could get a little mileage from the Either type. However the Either hierarchy is sealed and handling more than two types becomes cumbersome.

scala> implicit def string2either(s: String) = Left(s)
string2either: (s: String)Left[String,Nothing]

scala> implicit def int2either(i: Int) = Right(i)
int2either: (i: Int)Right[Nothing,Int]

scala> type SorI = Either[String, Int]
defined type alias SorI

scala> def foo(a: SorI) {a match {
     |     case Left(v)  => println("Got a "+v)
     |     case Right(v) => println("Got a "+v)
     |   }
     | }
foo: (a: SorI)Unit

scala> def bar(a: List[SorI]) {
     |   a foreach foo
     | }
bar: (a: List[SorI])Unit

scala>

scala> foo("Hello")
Got a Hello

scala> foo(10)
Got a 10

scala> bar(List(99, "beer"))
Got a 99
Got a beer
Don Mackenzie
+2  A: 

Another solution is wrapper classes:

case class IntList(l:List[Int])
case class StringList(l:List[String])

implicit def li2il(l:List[Int]) = IntList(l)
implicit def ls2sl(l:List[String]) = StringList(l)

def foo(list:IntList) =  { println("Int-List " + list.l)}
def foo(list:StringList) =  { println("String-List " + list.l)}
Landei