F#使用and递归绑定出现null问题,求修复方法
修复F#递归类型定义中的Null问题
原问题代码
type QueryInfo = { Title: string Check: Client -> bool Positive: Decision Negative: Decision } and Decision = | Result of string | Query of QueryInfo let rec tree1 = Query({Title=null;Check=(fun cl -> false);Positive=aaa; Negative=aaa}) and aaa = Result("SOME")
问题原因
F#处理相互递归的直接值绑定时,对于引用类型(比如Decision联合类型,底层为引用类型),会先为变量分配内存并初始化为null,再后续填充实际值。当tree1初始化时,aaa还未完成实际值的填充,因此Positive和Negative会被赋值为null。
修复方案
方案1:用函数延迟求值
将递归值改为函数,利用函数调用的延迟特性,确保求值时依赖项已完成初始化:
type QueryInfo = { Title: string Check: Client -> bool Positive: Decision Negative: Decision } and Decision = | Result of string | Query of QueryInfo let rec tree1 () = Query({Title=null;Check=(fun cl -> false);Positive=aaa(); Negative=aaa()}) and aaa () = Result("SOME")
使用时调用tree1()即可获取完整的Decision值。
方案2:用Lazy<T>延迟初始化
通过lazy包装递归值,在需要时再强制求值,避免提前引用未初始化的变量:
type QueryInfo = { Title: string Check: Client -> bool Positive: Decision Negative: Decision } and Decision = | Result of string | Query of QueryInfo let rec tree1 = lazy ( Query({Title=null;Check=(fun cl -> false);Positive=aaa.Value; Negative=aaa.Value}) ) and aaa = lazy (Result("SOME"))
使用时通过tree1.Value获取实际的Decision值。
方案3:自引用场景优化(若tree1需指向自身)
如果实际需求是让Positive和Negative指向tree1自身,可用as关键字简化:
type QueryInfo = { Title: string Check: Client -> bool Positive: Decision Negative: Decision } and Decision = | Result of string | Query of QueryInfo let rec tree1 = let rec info = { Title = null Check = (fun cl -> false) Positive = Query info Negative = Query info } Query info
内容的提问来源于stack exchange,提问作者dondublon
相关产品推荐
相关产品推荐

