【发布时间】:2021-04-11 15:57:19
【问题描述】:
我目前正在阅读 Enrico Buonanno 的 C# 函数式编程。第 3 章包含一个练习(我会将练习缩短到相关部分):
// 5. Write an implementations for the method in the `AppConfig` class below.
// Assume settings are of type string, numeric or date.
public class AppConfig
{
NameValueCollection source;
//public AppConfig() : this(ConfigurationManager.AppSettings) { }
public AppConfig(NameValueCollection source) => this.source = source;
public Option<T> Get<T>(string name)
{
throw new NotImplementedException("Your implementation here...");
}
}
我想出的解决方案是
public Option<T> Get<T>(string name)
{
try {
return (T)Convert.ChangeType(source.Get(name), typeof(T)));
}
catch {
return None;
};
}
作者的解决方法是
public Option<T> Get<T>(string key)
=> source[key] == null
? None
: Some((T)Convert.ChangeType(source[key], typeof(T)));
但这不是不诚实的功能吗?我可以有一个配置文件,其中包含一个值为“非数字”的键“SomeNumber”,所以如果我使用像Get<int>("SomeNumber") 这样的方法,这将抛出,而我的实现将返回 None。或者,我可能还有一个键为“SomeString”且值为“Goodbye World”的条目,我错误地尝试通过调用Get<int>("SomeString") 来检索它。
您可能会争辩说,抛出实际上是想要的结果,因为这是一个开发人员错误,应该突出显示。
想法?
【问题讨论】:
-
您可能会争辩说,抛出实际上是想要的结果,因为这是一个开发人员错误,应该突出显示。 – 是的。您可以用值而不是异常来表示它,但可能与缺失键的值不同。