假设我正在编写一个 GUI
class Kitteh (val age: Int) {
require (age < 5)
def saveMeow(file: File) = { /* implementation */ }
def savePurr(file: File) = { /* implementation */ }
}
该框架有一个当前 Kitteh 的字段,这是Option
因为它可能尚未定义,或者用户可能试图创建一个无效的:
var currentKitteh: Option[Kitteh] = None
现在我想Kitteh
在用户点击创建时安全地创建一个
val a = ... // parse age from text box
currentKitteh = try { Some(new Kitteh(a)) } catch { case _ => None }
我的 GUI 有两个按钮可以做类似的事情。在psedocode中,它们都应该
if (currentKitteh.isDefined) {
if (file on the disk already exists) {
bring up a dialog box asking for confirmation
if (user confirms)
<< execute method on currentKitteh >>
}
}
else bring up warning dialog
不要担心细节:关键是因为存在代码重复,我想创建一个可以从两个按钮调用的通用方法。唯一的区别是需要执行的 Kitteh 上的方法。
现在如果currentKitteh
不是 a Option
,通用方法可以有一个签名,如
def save(filename: String, f:(File => Unit)) {
例如,我可以打电话
save("meow.txt", currentKitteh.saveMeow _)
但由于它实际上是一个选项,我该如何实现呢?
我可以检查是否定义了 currentKitteh,并在为每个按钮.get
调用save
方法之前做一个,但是还有另一种方法,把这个检查留在save
方法中吗?换句话说,给定一个Option[A]
,是否可以从(可能不存在的)A
对象上的方法中指定部分函数?
(希望这个问题有意义,尽管有复杂的例子)
编辑:奖金问题:如果Option[Kitteh]
我使用了,而不是,怎么Either[Throwable, Kitteh]
办?
更新:添加到伪代码的附加行以显示警告对话框:理想情况下,save
应始终调用该方法,以便在没有有效的 Kitteh 可保存时警告用户。