C# 使用monad时,结果是来自语言ext的null异常
为什么会抛出“Result is null”异常?None不应该传播并将“a”设置为None吗C# 使用monad时,结果是来自语言ext的null异常,c#,monads,language-ext,C#,Monads,Language Ext,为什么会抛出“Result is null”异常?None不应该传播并将“a”设置为None吗 public void test() { Option<string> a = match( from b in ReturnNull() select b, x => x, () => null ); } private Option<string> ReturnNull() {
public void test()
{
Option<string> a = match(
from b in ReturnNull()
select b,
x => x,
() => null
);
}
private Option<string> ReturnNull()
{
return None;
}
公共无效测试()
{
选项a=匹配(
来自ReturnNull()中的b
选择b,
x=>x,
()=>空
);
}
私有选项ReturnNull()
{
不返回任何值;
}
因为null
不是选项的有效值。选项
的全部要点是避免空值和相关的空引用异常,但您所做的是在b
为无时分配空值:
Option<string> a = match(
from b in ReturnNull()
select b,
x => x,
// here
() => null
);
或者只使用var
,因为在这种情况下match
返回类型是string
当然,这段代码没有什么意义,因为您基本上只是扔掉选项
,然后返回到常规的可空字符串,但我假设这只是一个示例,而不是真正的代码
string a = match(
from b in ReturnNull()
select b,
x => x,
() => null
);