scala主要分为Keywords/reserved symbols
即关键字符号 和 被保留的 符号
Keywords
1
2
3
|
// Keywords <- // Used on for-comprehensions, to separate pattern from generator
= > // Used for function types, function literals and import renaming
|
我们在前面的博客对<- 和 =>有所提及,这里在简单描述一下。
<- 用于for逻辑内部 如:
1
|
for (p <- range if p > 5 ) yield p
|
=> 有三个比较常见的地方
1. 导入中用到
1
2
|
// 1. import import scala.{ Predef = > _ , _ } // 排除, 除了 Predef的其他所有类被导入
|
2. for 逻辑内部用到
1
2
|
// 2. for args.foreach ( arg = >greeting + = (arg + " " )) //for 章节中提到
|
3. match 逻辑内部用到
1
2
3
4
5
6
|
// 3. match case var a = 1 ;
a match {
case 1 = > 2 //a==1 则返回2
case 2 = > 5
} |
Reserved
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// Reserved ( ) // Delimit expressions and parameters
[ ] // Delimit type parameters
{ } // Delimit blocks
. // Method call and path separator
// /* */ // Comments # // Used in type notations
: // Type ascription or context bounds
< : > : < % // Upper, lower and view bounds
" " "" // Strings
' // Indicate symbols and characters
@ // Annotations and variable binding on pattern matching
` // Denote constant or enable arbitrary identifiers
, // Parameter separator
; // Statement separator
_ * // vararg expansion
_ // Many different meanings
|
译文:
被保留字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
( ) // 定界表达式和参数
[ ] // 定界参数类型
{ } // 定界块
. // 方法调用或者 路径分离
// /* */ // 注释 # // 被用于type标记
: // Type 归属或者 上下文边界
< : > : < % // 上升类型 下降类型 以及视图边界
" " "" // 字符串
' // 标识符号以及字符
@ // 在正则表达式之上的注解和绑定
` // 表示常数或启用任意标识符
, // 参数分隔器
; // 语句分隔器
_ * // 可变参数的扩展
_ // 不只有一个意思
|
<:举例
1
2
|
T < : A
//T是A的子类型(subtype) |
一个包括大部分符号的例子
1
2
3
4
5
6
7
8
9
10
11
12
|
package demo.scala
import scala. _ //导入scala包下所有类
object symbolDemo {
def sdemo(x : Int) : String = {
var sl : List[Int] = List( 1 , 2 );
"" //字符串 当前函数的返回值
} abstract class NativeType{
type JvmType
@ transient val tag : List[JvmType]
} } |
有多个含义的 -
1
2
3
4
5
6
7
8
9
10
11
|
import scala._ // Wild card -- all of Scala is imported
import scala.{ Predef => _, _ } // Exclusion, everything except Predef
def f[M[_]] // Higher kinded type parameter
def f(m: M[_]) // Existential type
_ + _ // Anonymous function placeholder parameter
m _ // Eta expansion of method into method value
m(_) // Partial function application
_ => 5 // Discarded parameter
case _ => // Wild card pattern -- matches anything
f(xs: _*) // Sequence xs is passed as multiple parameters to f(ys: T*)
case Seq(xs @ _*) // Identifier xs is bound to the whole matched sequence
|
译文:
1
2
3
4
5
6
7
8
9
10
11
|
import scala._ // 通配符 导入所有的scala类
import scala.{ Predef => _, _ } // 导入除Predef的所有类
def f[M[_]] // Higher kinded type 的参数 (Higher kinded type 特有的一种类型)
def f(m: M[_]) // Existential type (一种类型)
_ + _ // 匿名函数的参数占位符
m _ // Eta expansion of method into method value
m(_) // 部分函数应用
_ => 5 // 丢弃参数
case _ => // 通配符
f(xs: _*) // 序列xs是传递多个参数到 f(ys: T*)
case Seq(xs @ _*) // 标识符的xs被绑定到整个匹配的序列
|
好吧,写这章让我有点痛苦,下一章继续更新
本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/yjplxq/1412947,如需转载请自行联系原作者