Scala matching type

I want to write a general method to get an element at x position of a string or Int. Now my question is: how can I get Scala to convert T to Int or call a special Int method.

Here is the code:

def key[T](elm:T,pos:Int) = elm match{
  case x:Int => {
    def digit(number:Int, pos:Int):Int = {
      if(pos == 0) number % 10
        else digit(number/10, pos-1)
    }
    digit(elm.toInt,(elm.toInt).length-pos-1)
  } 
  case x:String => (elm.toString).charAt(pos)
}

Thank!

+3
source share
4 answers
def key[T](elm:T,pos:Int) = elm match { 
    case x:Int => x.toString.charAt(pos) 
    case s:String => s.charAt(pos) 
}

Solution using views:

implicit def i2s(i:Int):String = i.toString
def key2[T <% String](elm:T,pos:Int) = elm match { 
  case x:Int => x.charAt(pos) 
  case s:String => s.charAt(pos) 
}
+2
source

I would say your problem is that you have two types that you want to work on: Int and String, and you want to execute the key method on this input, but you will not indicate that you like to return.

My first idea for Int is to return Int. toString (). charAt (n) looks like a form of cheating, but perhaps the cheating is fine, so let's start cheating:

def cheat (a: Any, n: Int) = a.toString ().charAt (n) 
cheat (2345, 2) 
// res414: Char = 4
cheat ("foobar", 2) 
// res415: Char = o

, ? , Int Int String ? Char String, 0 9, , T T.

, , - , ?

class Atti [T] (val fun: (Int => T)) {
  def at (n: Int): T = fun (n) 
}

-, T , Int T, at , .

iGet, Int n, ,...

def iGet (i: Int) (n: Int) : Int = {
  if (n == 0) i % 10
    else iGet (i / 10)(n - 1) }

... Int, , :

val iAt = new Atti[Int] (iGet (2345) _)
 (0 to 3).map (iAt.at (_))
// res412: scala.collection.immutable.IndexedSeq[Int] = Vector(5, 4, 3, 2)

sAt:

def sGet (s: String) (n: Int) : String = "" + s.charAt (n)

val sAt = new Atti[String] (sGet ("foobar") _) 
 (0 to 3).map (sAt.at (_))
// res413: scala.collection.immutable.IndexedSeq[String] = Vector(f, o, o, b)

, , String , Int , .

, , sAt foobar String, 2345 - iAt-.

, , , , , .

Int String, :

trait Key [T] { def at (n: Int): T }
val s : String with Key[String] = "Foobar" { def at (n: Int) : String = "" + s.charAt (n) }  
<console>:7: error: type mismatch;
 found   : Unit
 required: Int
       val s : String with Key[String] = "Foobar" { def at (n: Int) : String = "" + s.charAt (n) }
                                                                                                 ^
+1
def key[T](elm:T,pos:Int) = elm match {
  case x:Int => {
    def digit(number:Int, pos:Int):Int = {
      if(pos == 0) number % 10
        else digit(number/10, pos-1)
      }
    digit(x,x.length-pos-1)
  } 
  case x:String => x.charAt(pos)
}
0
source

how can i get scala to convert t to int

You can not. Well, you can with some implicit validation, but in this case is more complicated than you need. elmUse instead x:

def key[T](elm:T,pos:Int) = elm match{
  case x:Int => {
    def digit(number:Int, pos:Int):Int = {
      if(pos == 0) number % 10
        else digit(number/10, pos-1)
    }
    digit(x,x.toString.length-pos-1)
  } 
  case x:String => x.charAt(pos)
}
0
source

All Articles