scala 操作符=方法
2013-06-30 11:40:42| 分类:
Scala
| 标签:
|举报
|字号大中小 订阅
scala> val s = "Hello, world!"
s: String = Hello, world!
scala> s indexOf 'o' //中缀操作=s.indexOf('o')
res6: Int = 4
scala> s indexOf ('o', 5)
res7: Int = 8
scala> s toLowerCase //后缀操作=s.toLowerCase()
res8: String = hello, world!
--------------------------------------------------------------------------------------------
Any method can be an operator
In Scala operators are not special language syntax: any method can
be an operator. What makes a method an operator is how you use it.
When you write “s.indexOf('o')”, indexOf is not an operator. But
when you write “s indexOf 'o'”, indexOf is an operator, because
you’re using it in operator notation.
--------------------------------------------------------------------------------------------
0 max 5 = 5
0 min 5 = 0
-2.7 abs = 2.7
2.7 round =3L
1.5 isInfinity =false
(1.0 / 0) isInfinity= true
4 to 6 = Range(4, 5, 6)
"bob" capitalize ="Bob"
"robert" drop 2 = "bert"
评论这张
转发至微博
转发至微博
评论