Problems with Pattern matching, implementing SplitAt in scala

前端 未结 1 1014
眼角桃花
眼角桃花 2020-12-12 06:05

I am trying to implement the scala splitAt using pattern matching and this is what I am trying to do:

def split[T](someIndex:Int,someList:List[T]):(List[T],L         


        
相关标签:
1条回答
  • 2020-12-12 06:53

    You should use `someIndex` and `currentIndex` (constants) in pattern matching.

    scala> val a = 1
    a: Int = 1
    
    scala> 2 match {
         |   case a => println(a)
         | }
    2
    
    scala> 2 match {
         |   case `a` => println("a")
         |   case _ => println("Oops")
         | }
    Oops
    

    Chapter 15 of Programming in Scala, First Edition. "Case Classes and Pattern Matching" by Martin Odersky, Lex Spoon, and Bill Venners:

    If you need to, you can still use a lowercase name for a pattern constant, using one of two tricks. First, if the constant is a field of some object, you can prefix it with a qualifier. For instance, pi is a variable pattern, but this.pi or obj.pi are constants even though they start with lowercase letters. If that does not work (because pi is a local variable, say), you can alternatively enclose the variable name in back ticks.

    0 讨论(0)
提交回复
热议问题