Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I setup multiple ORed type bounds in Scala

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)

like image 680
oluies Avatar asked Sep 24 '10 21:09

oluies


4 Answers

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
like image 151
Daniel C. Sobral Avatar answered Nov 05 '22 00:11

Daniel C. Sobral


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
like image 22
Don Mackenzie Avatar answered Nov 04 '22 23:11

Don Mackenzie


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)}
like image 2
Landei Avatar answered Nov 04 '22 23:11

Landei


There is this hack:

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.

like image 1
michid Avatar answered Nov 05 '22 01:11

michid