我习惯用C#编写这样的代码:
SomeObj obj;
try{
// this may throw SomeException
obj = GetSomeObj();
}catch(SomeException){
// Log error...
obj = GetSomeDefaultValue();
}
obj.DoSomething();
这是我在 F# 中翻译的方式(obj 是一个列表):
let mutable obj = []
try
obj <- getSomeObj
with
| ex ->
// Log ex
obj <- getSomeDefaultValue
doSomething obj
在不使用可变变量的情况下,有没有办法在F#中实现这一点?在F#中处理这种情况是否有更“优雅”的方式?
谢谢!