eyword indicates that the corresponding object can be used
* implicitly, within this scope, as a parameter of a function marked implicit.
*/
implicit object StringMonoid extends Monoid[String] {
def add(x: String, y: String): String = x concat y
def unit: String = ""
}
implicit object IntMonoid extends Monoid[Int] {
def add(x: Int, y: Int): Int = x + y
def unit: Int = 0
}
/**
* This method takes a List[A] returns an A which represent the combined
* value of applying the monoid operation successively across the whole list.
* Making the parameter m implicit here means we only have to provide
* the xs parameter at the call site, since if we have a List[A]
* we know what type A actually is and therefore what type Monoid[A] is needed.
* We can then implicitly find whichever val or object in the current scope
* also has that type and use that without needing to specify it explicitly.
*/
def sum[A](xs: List[A])(implicit m: Monoid[A]): A =
if (xs.isEmpty) m.unit
else m.add(xs.head, sum(xs.tail))
/**
* Here we call sum twice, with only one parameter each time.
* Since the second parameter of sum, m, is implicit its value is looked up
* in the current scope, based on the type of monoid required in each case,
* meaning both expressions can be fully eva luated.
*/
println(sum(List(1, 2, 3))) // uses IntMonoid implicitly
println(sum(List("a", "b", "c"))) // uses StringMonoid implicitly
}
implicit的功能可以分为两类:
implicit conversions (from implicit functions and implicit classes) 当Scala发现类型不匹配,或者正在调用一个对象不存在的函数时, Scala compiler就会去implicit function list中找一个匹配的函数。
implicit arguments (from implicit values and implicit objects) 在函数上可以定义一个implcit参数,编译器会在implicit的对象列表中, 找到一个类型匹配的对象,并传入。
作用域
可以想到,implicit有个作用域。这个作用域,和当前的package,以及import的类, 还有Scala的默认有关。
Level 3: 重申:避免使用implicit
Level 4: 如果要使用implicit
- 好好阅读关于implicit作用域,编译器的查找complicit对象的顺序等知识。
- 定义implicit的开发规范
- 写好文档帮助开发人员和用户理解。
- 限制其使用的场景
Collection
请看Scala Collection简介
Mutability vs Immutability
可变的变量(Mutable variables)(主要是可变的对象)会引起一些潜在的问题:
- 变化后,可能在map中找不到了
- 性能损失(在多线程下,读写需要加锁)
编程方面的建议是:
杂七杂八
_ 的用途
请请看Scala underscore的用途
Null, null, Nil, Nothing, None, and Unit
Null是Scala中的一个Trait.
null是一个Null的实例,相当于Java中的null。在面向函数编程中,不要使用,用None代替。
None是一个None.type的实例,代替null。 在一个函数中,用于表达这个函数返回了没有值。可以避免 null pointer exception。
Unit是Scala中的一个类型,用于表示一个函数没有返回值。 有点像Java中的void,不过其实返回了'()'。
// There is no '=' before '{', so the function return Unit.
def funUnit(x: Int) {
x * x
}
// There is a '=' before '{',
// so the function returns the value of the last expression.
def funReturnLastStatement(x: Int) = {
x * x
x + x
}
// Recommend to use explicit return type, and = forever.
def funReturnLastStatementGood(x: Int) : Int = {
x * x
x + x
}
- Nil 是一个空的List实例.
- Nothing是Scala中的一个Trait。基本上没有用。(我猜的)
## 和 == 对 equals 和 hashCode
在Scala中, ##方法相当于Java中的hashCode方法。 ==方法相当于Java中的equals方法。 建议使用##和==,因为Scala针对value类型实现额外的功能。
Generic classes/functions: +T, -T, <:, >:, <&
- +T: 作用于class, 用于协变(covariant)
-T:作用于class, 用于逆变(contravariant)
+T, -T 作用于泛型对象之间赋值。
- T <: A: 作用于 function, 约束:T是A的子类,也称作upper type bound,这是一个协变。
一般会和-T合用。
- T >: A: 作用于 function, 约束:T是A的超类,也称作lower type bound,这是一个逆变。
一般会和+T合用。
T <& A: 作用于 function, 约束:存在一个T => A的implicit conversion,也称作view bound.
<:, >:, <& 作用于泛型函数之间 |