Bottom/undefined value in F#?

半城伤御伤魂 提交于 2019-12-12 08:45:48

问题


There is a convenient undefined value in Haskell that can be used as a stub for yet to be defined functions/paths in code. Is there anything like it in F#?


回答1:


To be concrete, you can define such a value like this:

let undefined<'T> : 'T = failwith "Not implemented yet"

let stub1 (x : int) : float = undefined
let stub2 (x : 'T) : 'T = undefined

Beware that F# evaluation is strict. If you bind undefined to a top-level value, it will throw an exception during evaluation.




回答2:


I think

failwith "Not implemented"

would be pretty much equivalent




回答3:


More specific and .NET-friendly way

let undefined<'T> : 'T = raise (NotImplementedException())

allows you to skip typing a message and still differentiate this exception from other ones in a catch block or stack trace.



来源:https://stackoverflow.com/questions/20337249/bottom-undefined-value-in-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!