scala 基本语法-4 ok

来源:互联网 发布:淘宝的主营类目怎么改 编辑:程序博客网 时间:2024/06/05 05:45
[root@cdh1 conf]# cd /user/local/test1
[root@cdh1 test1]# ll
total 32
-rw-r--r-- 1 root root  216 Jul 11 19:35 matchTest.scala
-rw-r--r-- 1 root root  697 Jul 11 06:30 prt1.class
-rw-r--r-- 1 root root 1066 Jul 11 06:30 prt1$.class
-rw-r--r-- 1 root root  586 Jul 11 06:29 prt1.scala
-rw-r--r-- 1 root root  594 Jul 11 06:26 prt.class
-rw-r--r-- 1 root root 1052 Jul 11 06:26 prt$.class
-rw-r--r-- 1 root root  120 Jul 11 06:19 prt.scala
-rw-r--r-- 1 root root   21 Jul 11 23:22 test.txt
[root@cdh1 test1]# vim fileList.scala
[root@cdh1 test1]# scalac fileList.scala 
fileList.scala:3: error: not found: value filesHere
 for (file <- filesHere) 
              ^
one error found
[root@cdh1 test1]# vim fileList.scala
[root@cdh1 test1]# scalac fileList.scala
[root@cdh1 test1]# scala fileList.scala
./prt1.scala
./prt.scala
./fileList.scala
./matchTest.scala
[root@cdh1 test1]# ls
fileList$$anonfun$main$1.class  fileList$.class  matchTest.scala  prt1$.class  prt.class   prt.scala
fileList.class                  fileList.scala   prt1.class       prt1.scala   prt$.class  test.txt
[root@cdh1 test1]# ls -l
total 48
-rw-r--r-- 1 root root 1130 Jul 12 04:53 fileList$$anonfun$main$1.class
-rw-r--r-- 1 root root  685 Jul 12 04:53 fileList.class
-rw-r--r-- 1 root root  968 Jul 12 04:53 fileList$.class
-rw-r--r-- 1 root root  244 Jul 12 04:53 fileList.scala
-rw-r--r-- 1 root root  216 Jul 11 19:35 matchTest.scala
-rw-r--r-- 1 root root  697 Jul 11 06:30 prt1.class
-rw-r--r-- 1 root root 1066 Jul 11 06:30 prt1$.class
-rw-r--r-- 1 root root  586 Jul 11 06:29 prt1.scala
-rw-r--r-- 1 root root  594 Jul 11 06:26 prt.class
-rw-r--r-- 1 root root 1052 Jul 11 06:26 prt$.class
-rw-r--r-- 1 root root  120 Jul 11 06:19 prt.scala
-rw-r--r-- 1 root root   21 Jul 11 23:22 test.txt
[root@cdh1 test1]# cat fileList.scala
object fileList{
 //val filesHere = (new java.io.File(".")).listFiles
 def main(args :Array[String]){
  val filesHere = (new java.io.File(".")).listFiles
  for (file <- filesHere) 
    if (file.getName.endsWith(".scala")) 
  println(file)
 }
}
[root@cdh1 test1]# 




[root@cdh1 conf]# cd /user/local/test1
[root@cdh1 test1]# ll
total 32
-rw-r--r-- 1 root root  216 Jul 11 19:35 matchTest.scala
-rw-r--r-- 1 root root  697 Jul 11 06:30 prt1.class
-rw-r--r-- 1 root root 1066 Jul 11 06:30 prt1$.class
-rw-r--r-- 1 root root  586 Jul 11 06:29 prt1.scala
-rw-r--r-- 1 root root  594 Jul 11 06:26 prt.class
-rw-r--r-- 1 root root 1052 Jul 11 06:26 prt$.class
-rw-r--r-- 1 root root  120 Jul 11 06:19 prt.scala
-rw-r--r-- 1 root root   21 Jul 11 23:22 test.txt
[root@cdh1 test1]# vim fileList.scala
[root@cdh1 test1]# scalac fileList.scala 
fileList.scala:3: error: not found: value filesHere
 for (file <- filesHere) 
              ^
one error found
[root@cdh1 test1]# vim fileList.scala
[root@cdh1 test1]# scalac fileList.scala
[root@cdh1 test1]# scala fileList.scala
./prt1.scala
./prt.scala
./fileList.scala
./matchTest.scala
[root@cdh1 test1]# ls
fileList$$anonfun$main$1.class  fileList$.class  matchTest.scala  prt1$.class  prt.class   prt.scala
fileList.class                  fileList.scala   prt1.class       prt1.scala   prt$.class  test.txt
[root@cdh1 test1]# ls -l
total 48
-rw-r--r-- 1 root root 1130 Jul 12 04:53 fileList$$anonfun$main$1.class
-rw-r--r-- 1 root root  685 Jul 12 04:53 fileList.class
-rw-r--r-- 1 root root  968 Jul 12 04:53 fileList$.class
-rw-r--r-- 1 root root  244 Jul 12 04:53 fileList.scala
-rw-r--r-- 1 root root  216 Jul 11 19:35 matchTest.scala
-rw-r--r-- 1 root root  697 Jul 11 06:30 prt1.class
-rw-r--r-- 1 root root 1066 Jul 11 06:30 prt1$.class
-rw-r--r-- 1 root root  586 Jul 11 06:29 prt1.scala
-rw-r--r-- 1 root root  594 Jul 11 06:26 prt.class
-rw-r--r-- 1 root root 1052 Jul 11 06:26 prt$.class
-rw-r--r-- 1 root root  120 Jul 11 06:19 prt.scala
-rw-r--r-- 1 root root   21 Jul 11 23:22 test.txt
[root@cdh1 test1]# cat fileList.scala
object fileList{
 //val filesHere = (new java.io.File(".")).listFiles
 def main(args :Array[String]){
  val filesHere = (new java.io.File(".")).listFiles
  for (file <- filesHere) 
    if (file.getName.endsWith(".scala")) 
  println(file)
 }
}
[root@cdh1 test1]# ll
total 48
-rw-r--r-- 1 root root 1130 Jul 12 04:53 fileList$$anonfun$main$1.class
-rw-r--r-- 1 root root  685 Jul 12 04:53 fileList.class
-rw-r--r-- 1 root root  968 Jul 12 04:53 fileList$.class
-rw-r--r-- 1 root root  244 Jul 12 04:53 fileList.scala
-rw-r--r-- 1 root root  216 Jul 11 19:35 matchTest.scala
-rw-r--r-- 1 root root  697 Jul 11 06:30 prt1.class
-rw-r--r-- 1 root root 1066 Jul 11 06:30 prt1$.class
-rw-r--r-- 1 root root  586 Jul 11 06:29 prt1.scala
-rw-r--r-- 1 root root  594 Jul 11 06:26 prt.class
-rw-r--r-- 1 root root 1052 Jul 11 06:26 prt$.class
-rw-r--r-- 1 root root  120 Jul 11 06:19 prt.scala
-rw-r--r-- 1 root root   21 Jul 11 23:22 test.txt
[root@cdh1 test1]# mkdir a.scala
[root@cdh1 test1]# ll
total 52
drwxr-xr-x 2 root root 4096 Jul 12 04:59 a.scala
-rw-r--r-- 1 root root 1130 Jul 12 04:53 fileList$$anonfun$main$1.class
-rw-r--r-- 1 root root  685 Jul 12 04:53 fileList.class
-rw-r--r-- 1 root root  968 Jul 12 04:53 fileList$.class
-rw-r--r-- 1 root root  244 Jul 12 04:53 fileList.scala
-rw-r--r-- 1 root root  216 Jul 11 19:35 matchTest.scala
-rw-r--r-- 1 root root  697 Jul 11 06:30 prt1.class
-rw-r--r-- 1 root root 1066 Jul 11 06:30 prt1$.class
-rw-r--r-- 1 root root  586 Jul 11 06:29 prt1.scala
-rw-r--r-- 1 root root  594 Jul 11 06:26 prt.class
-rw-r--r-- 1 root root 1052 Jul 11 06:26 prt$.class
-rw-r--r-- 1 root root  120 Jul 11 06:19 prt.scala
-rw-r--r-- 1 root root   21 Jul 11 23:22 test.txt
[root@cdh1 test1]# scala fileList.scala
./prt1.scala
./prt.scala
./fileList.scala
./a.scala
./matchTest.scala
[root@cdh1 test1]# vim fileList.scala
[root@cdh1 test1]# scalac fileList.scala
fileList.scala:6: error: illegal start of simple expression
    if (file.isFile); 
                    ^
one error found
[root@cdh1 test1]# vim fileList.scala
[root@cdh1 test1]# scalac fileList.scala
[root@cdh1 test1]# scala fileList.scala
./prt1.scala
./prt.scala
./fileList.scala
./matchTest.scala
[root@cdh1 test1]# cp fileList.scala fileList2.scala
[root@cdh1 test1]# vim fileList2.scala
\[root@cdh1 test1]#scalac fileList2.scala
fileList2.scala:7: error: illegal start of simple pattern
     if (file.isFile); 
     ^
fileList2.scala:7: error: '<-' expected but ';' found.
     if (file.isFile); 
                     ^
fileList2.scala:10: error: '<-' expected but '}' found.
  }
  ^
fileList2.scala:11: error: illegal start of simple expression
 }
 ^
four errors found
[root@cdh1 test1]# vim fileList2.scala
[root@cdh1 test1]# scalac fileList2.scala
[root@cdh1 test1]# scala fileList2.scala
./prt1.scala
./prt.scala
./fileList.scala
./matchTest.scala
./fileList2.scala
[root@cdh1 test1]# cat fileList2.scala
object fileList2{
 //val filesHere = (new java.io.File(".")).listFiles
 def main(args :Array[String]){
  val filesHere = (new java.io.File(".")).listFiles
  //for (file <- filesHere)
  for(
     file <- filesHere
     if (file.isFile); 
     if (file.getName.endsWith(".scala")) 
     //println(file)
  )
   println(file)
 }
}
[root@cdh1 test1]# cp fileList2.scala fileList3.scala
[root@cdh1 test1]# vim fileList3.scala
[root@cdh1 test1]# scalac fileList3.scala
fileList3.scala:6: error: ';' expected but '<-' found.
     file <- filesHere
          ^
fileList3.scala:9: error: illegal start of simple pattern
     if (file.isFile); 
     ^
fileList3.scala:9: error: '<-' expected but ';' found.
     if (file.isFile); 
                     ^
three errors found
[root@cdh1 test1]# vim fileList3.scala
[root@cdh1 test1]# scalac fileList3.scala
[root@cdh1 test1]# scala fileList3.scala
./prt1.scala
./prt.scala
./fileList.scala
./matchTest.scala
./fileList2.scala
./fileList3.scala
[root@cdh1 test1]# cat fileList3.scala
object fileList3{
 //val filesHere = (new java.io.File(".")).listFiles
 def main(args :Array[String]){
  val filesHere = (new java.io.File(".")).listFiles
  //for (file <- filesHere)
     //file <- filesHere
  for(file <- filesHere if (file.isFile); if (file.getName.endsWith(".scala")) )
   println(file)
 }
}
[root@cdh1 test1]# vim fileList3.scala
[root@cdh1 test1]# scalac fileList3.scala
[root@cdh1 test1]# scala fileList3.scala
./prt1.scala
./prt.scala
./fileList.scala
./matchTest.scala
./fileList2.scala
./fileList3.scala
[root@cdh1 test1]# cat fileList3.scala
object fileList3{
 //val filesHere = (new java.io.File(".")).listFiles
 def main(args :Array[String]){
  val filesHere = (new java.io.File(".")).listFiles
  //for (file <- filesHere)
     //file <- filesHere
  for(file <- filesHere; if (file.isFile); if (file.getName.endsWith(".scala")) )
   println(file)
 }
}
[root@cdh1 test1]# ll
total 100
drwxr-xr-x 2 root root 4096 Jul 12 04:59 a.scala
-rw-r--r-- 1 root root  921 Jul 12 05:17 fileList2$$anonfun$main$1.class
-rw-r--r-- 1 root root 1023 Jul 12 05:17 fileList2$$anonfun$main$2.class
-rw-r--r-- 1 root root  992 Jul 12 05:17 fileList2$$anonfun$main$3.class
-rw-r--r-- 1 root root  770 Jul 12 05:17 fileList2.class
-rw-r--r-- 1 root root 1214 Jul 12 05:17 fileList2$.class
-rw-r--r-- 1 root root  327 Jul 12 05:17 fileList2.scala
-rw-r--r-- 1 root root  921 Jul 12 05:24 fileList3$$anonfun$main$1.class
-rw-r--r-- 1 root root 1023 Jul 12 05:24 fileList3$$anonfun$main$2.class
-rw-r--r-- 1 root root  992 Jul 12 05:24 fileList3$$anonfun$main$3.class
-rw-r--r-- 1 root root  770 Jul 12 05:24 fileList3.class
-rw-r--r-- 1 root root 1214 Jul 12 05:24 fileList3$.class
-rw-r--r-- 1 root root  312 Jul 12 05:24 fileList3.scala
-rw-r--r-- 1 root root 1166 Jul 12 05:02 fileList$$anonfun$main$1.class
-rw-r--r-- 1 root root  685 Jul 12 05:02 fileList.class
-rw-r--r-- 1 root root  968 Jul 12 05:02 fileList$.class
-rw-r--r-- 1 root root  270 Jul 12 05:02 fileList.scala
-rw-r--r-- 1 root root  216 Jul 11 19:35 matchTest.scala
-rw-r--r-- 1 root root  697 Jul 11 06:30 prt1.class
-rw-r--r-- 1 root root 1066 Jul 11 06:30 prt1$.class
-rw-r--r-- 1 root root  586 Jul 11 06:29 prt1.scala
-rw-r--r-- 1 root root  594 Jul 11 06:26 prt.class
-rw-r--r-- 1 root root 1052 Jul 11 06:26 prt$.class
-rw-r--r-- 1 root root  120 Jul 11 06:19 prt.scala
-rw-r--r-- 1 root root   21 Jul 11 23:22 test.txt
[root@cdh1 test1]# less matchTest.scala 
[root@cdh1 test1]# vim matchTest.scala 
[root@cdh1 test1]# scalac matchTest.scala 
[root@cdh1 test1]# scala matchTest.scala 
huh?
[root@cdh1 test1]# scala matchTest.scala chips
salsa
[root@cdh1 test1]# scala matchTest.scala salt
pepper
[root@cdh1 test1]# scala matchTest.scala eggs
bacon
[root@cdh1 test1]# scala matchTest.scala 
huh?
[root@cdh1 test1]# cat matchTest.scala 
object matchTest{
 def main(args:Array[String]){
   val firstArg = 
   if(args.length > 0) 
     args(0) 
   else 
     "" 
   firstArg match { 
   case "salt"  => println("pepper") 
   case "chips" => println("salsa") 
   case "eggs"  => println("bacon") 
   case _ => println("huh?") }
  }
}
[root@cdh1 test1]# scala matchTest.scala eggs salt
bacon
[root@cdh1 test1]# scala matchTest.scala matchTest.
matchTest.class  matchTest.scala  
[root@cdh1 test1]# cp matchTest.scala matchTest2.scala
[root@cdh1 test1]# vim matchTest2.scala 


[1]+  Stopped                 vim matchTest2.scala
[root@cdh1 test1]# vim matchTest2.scala
[root@cdh1 test1]# scalac matchTest2.scala
[root@cdh1 test1]# scala matchTest2.scala eggs salt
e_bacon
[root@cdh1 test1]# scala matchTest2.scala salt
s_pepper
[root@cdh1 test1]# scala matchTest2.scala chips
c_salsa
[root@cdh1 test1]# scala matchTest2.scala chipsss
o_huh?
[root@cdh1 test1]# scala matchTest2.scala 
o_huh?
[root@cdh1 test1]# cat matchTest2.scala 
object matchTest2{
 def main(args:Array[String]){
   val firstArg = 
   if(args.length > 0) 
     args(0) 
   else 
     "" 
   var friend=
    firstArg match { 
    case "salt"  => "s_pepper" 
    case "chips" => "c_salsa" 
    case "eggs"  => "e_bacon" 
    case _ => "o_huh?" }
   println(friend)
  }
}
[root@cdh1 test1]# scala
Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67).
Type in expressions to have them evaluated.
Type :help for more information.


scala> var increase = (x: Int) => x + 1
increase: Int => Int = <function1>


scala> increase 10
<console>:1: error: ';' expected but integer literal found.
       increase 10
                ^


scala> increase (10)
res0: Int = 11


scala> 


scala> increase (10)
res1: Int = 11


scala> var increase = (x: Int) => x + 9999
increase: Int => Int = <function1>


scala> increase (10)
res2: Int = 10009


scala> val increase = (x: Int) => x + 1
increase: Int => Int = <function1>


scala> increase (10)
res3: Int = 11


scala>  increase = (x: Int) => x + 9999
<console>:8: error: reassignment to val
        increase = (x: Int) => x + 9999
                 ^


scala> var increase = (x: Int) => x + 1
increase: Int => Int = <function1>


scala>  increase = (x: Int) => x + 9999
increase: Int => Int = <function1>


scala> increase (10)
res4: Int = 10009


scala> increase = (x: Int) => { println("We") println("are") println("here!") x + 1 }
<console>:8: error: value println is not a member of Unit
       increase = (x: Int) => { println("We") println("are") println("here!") x + 1 }
                                              ^


scala> increase = (x: Int) => { println("We") println("are") println("here!") x + 1 }
<console>:8: error: value println is not a member of Unit
       increase = (x: Int) => { println("We") println("are") println("here!") x + 1 }
                                              ^


scala> increase = (x: Int) => { println("We") ;println("are"); println("here!"); x + 1 }
increase: Int => Int = <function1>


scala> increase (10)
We
are
here!
res5: Int = 11


scala> val someNumbers = List(-11, -10, -5, 0, 5, 10)
someNumbers: List[Int] = List(-11, -10, -5, 0, 5, 10)


scala> someNumbers.f
filter            filterNot         find              findIndexOf       findLastIndexOf   first             firstOption       
flatMap           flatten           fold              foldLeft          foldRight         forall            foreach           


scala> someNumbers.foreach((x:Int)->println(x))
<console>:9: error: not found: value x
              someNumbers.foreach((x:Int)->println(x))
                                   ^


scala> someNumbers.foreach((x:Int)=>println(x))
-11
-10
-5
0
5
10


scala> someNumbers
res8: List[Int] = List(-11, -10, -5, 0, 5, 10)


scala> someNumbers.filter
filter      filterNot   


scala> someNumbers.filter((x:Int)=>x>0)
res9: List[Int] = List(5, 10)


scala> someNumbers.filter((x:Int)=>x>0).foreach((_:Int)=>println(_))
<console>:9: error: missing parameter type for expanded function ((x$2) => println(x$2))
              someNumbers.filter((x:Int)=>x>0).foreach((_:Int)=>println(_))
                                                                        ^


scala> someNumbers.filter((x:Int)=>x>0).foreach((x:Int)=>println(x))
5
10


scala> someNumbers.filter((x:Int)=>x>0).foreach((y:Int)=>println(y))
5
10


scala> someNumbers.filter((x)=>x>0)
res13: List[Int] = List(5, 10)


scala> someNumbers.filter(x=>x>0)
res14: List[Int] = List(5, 10)


scala> someNumbers.filter(x=>x>0).foreach((y:Int)=>println(y))
5
10


scala> someNumbers.filter(_>0).foreach(println(_))
5
10


scala> someNumbers.filter(_>0)
res17: List[Int] = List(5, 10)


scala> someNumbers.filter(_<0)
res18: List[Int] = List(-11, -10, -5)


scala> someNumbers.filter(_<0).foreach(println(_))
-11
-10
-5


scala> 






scala> val f =_+_
<console>:7: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2))
       val f =_+_
              ^
<console>:7: error: missing parameter type for expanded function ((x$1: <error>, x$2) => x$1.$plus(x$2))
       val f =_+_
                ^


scala> val f =(_:Int)+(_:InT)
<console>:7: error: not found: type InT
       val f =(_:Int)+(_:InT)
                         ^


scala> val f =(_:Int)+(_:Int)
f: (Int, Int) => Int = <function2>


scala> f(2,3)
res20: Int = 5


scala> def sum(a: Int, b: Int, c: Int) = a + b + c
sum: (a: Int, b: Int, c: Int)Int


scala> sum(1,3,5)
res21: Int = 9


scala> val f=sum _
f: (Int, Int, Int) => Int = <function3>


scala> sum(1,2)
<console>:9: error: not enough arguments for method sum: (a: Int, b: Int, c: Int)Int.
Unspecified value parameter c.
              sum(1,2)
                 ^


scala> f(1,2)
<console>:10: error: not enough arguments for method apply: (v1: Int, v2: Int, v3: Int)Int in trait Function3.
Unspecified value parameter v3.
              f(1,2)
               ^


scala> f(1,2,3)
res24: Int = 6


scala> f.a
apply          asInstanceOf   


scala> f.apply(1,2,4)
res25: Int = 7


scala> f.apply(1,2,3)
res26: Int = 6


scala> val b = sum(1, _: Int, 3)
b: Int => Int = <function1>


scala> b(2)
res27: Int = 6


scala> b(5)
res28: Int = 9


scala> b(4)
res29: Int = 8


scala> someNumbers
res30: List[Int] = List(-11, -10, -5, 0, 5, 10)


scala> someNumbers.foreach(println)
-11
-10
-5
0
5
10


scala> someNumbers.filter(>0)foreach(println)
<console>:1: error: ')' expected but integer literal found.
       someNumbers.filter(>0)foreach(println)
                           ^


scala> someNumbers.filter(>0).foreach(println)
<console>:1: error: ')' expected but integer literal found.
       someNumbers.filter(>0).foreach(println)
                           ^


scala> someNumbers.filter(_>0).foreach(println)
5
10


scala> val c = sum
<console>:8: error: missing arguments for method sum in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
       val c = sum
               ^


scala> val c = sum _
c: (Int, Int, Int) => Int = <function3>


scala> c(2,3,4)
res33: Int = 9


scala> (x: Int) => x + more
<console>:8: error: not found: value more
              (x: Int) => x + more
                              ^


scala> var more = 1
more: Int = 1


scala> val f(x: Int) => x + more
<console>:1: error: '=' expected but '=>' found.
       val f(x: Int) => x + more
                     ^


scala> val f(x: Int) = x + more
<console>:10: error: value f is not a case class constructor, nor does it have an unapply/unapplySeq method
       val f(x: Int) = x + more
           ^


scala> val f=(x: Int) => x + more
f: Int => Int = <function1>


scala> f(1)
res35: Int = 2


scala> f(3)
res36: Int = 4


scala> f(10)
res37: Int = 11


scala> more = 9999
more: Int = 9999


scala> f(10)
res38: Int = 10009


scala> someNumbers
res39: List[Int] = List(-11, -10, -5, 0, 5, 10)


scala> someNumbers.foreach(sum+=_)
<console>:10: error: missing arguments for method sum in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
              someNumbers.foreach(sum+=_)
                                  ^
<console>:10: error: reassignment to val
              someNumbers.foreach(sum+=_)
                                     ^


scala> var sum=0;
sum: Int = 0


scala> someNumbers.foreach(sum+=_)


scala> someNumbers
res42: List[Int] = List(-11, -10, -5, 0, 5, 10)


scala> someNumbers.foreach(sum+=_)


scala> someNumbers.foreach(sum+=_)


scala> sum
res45: Int = -33


scala> someNumbers.foreach(sum+=_).println
<console>:10: error: value println is not a member of Unit
              someNumbers.foreach(sum+=_).println
                                          ^


scala> def echo(args:String*)
     | args.foreach(println)
<console>:10: error: not found: value args
              args.foreach(println)
              ^
<console>:7: error: only classes can have declared but undefined members
              def echo(args:String*)
                  ^


scala> val echo(args:String*)
<console>:1: error: ')' expected but identifier found.
       val echo(args:String*)
                           ^


scala> val echo(args:String*) args.foreach(println)
<console>:1: error: ')' expected but identifier found.
       val echo(args:String*) args.foreach(println)
                           ^


scala> def echo(args:String*)=args.foreach(println)
echo: (args: String*)Unit


scala> echo("hello","world")
hello
world


scala> def echo(args:String*)=for(arg<-args) println
echo: (args: String*)Unit


scala> echo("hello","world")






scala> echo("hello","world")






scala> def echo(args:String*)=for(arg<-args) println(_)
<console>:7: error: missing parameter type for expanded function ((x$1) => println(x$1))
       def echo(args:String*)=for(arg<-args) println(_)
                                                     ^


scala> def echo(args:String*)=for(arg<-args) println(arg)
echo: (args: String*)Unit


scala> echo("hello","world")
hello
world


scala> echo("hello","world","spark")
hello
world
spark


scala> echo()


scala> echo("hello")
hello


scala> val arr = Array("What's", "up", "doc?")
arr: Array[java.lang.String] = Array(What's, up, doc?)


scala> echo(arr)
<console>:10: error: type mismatch;
 found   : Array[java.lang.String]
 required: String
              echo(arr)
                   ^


scala> echo(arr.toString)
[Ljava.lang.String;@614169


scala> echo(arr.toString:_*)
<console>:10: error: type mismatch;
 found   : java.lang.String
 required: Seq[String]
              echo(arr.toString:_*)
                       ^


scala> echo(arr:_*)
What's
up
doc?


scala> arr
res59: Array[java.lang.String] = Array(What's, up, doc?)


scala> echo(arr:_)
<console>:1: error: `*' expected
       echo(arr:_)
                 ^


scala> echo(arr:_*)
What's
up
doc?


scala> 

0 0
原创粉丝点击