Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottom/undefined value in F#?

Tags:

f#

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#?

like image 406
Trident D'Gao Avatar asked Dec 02 '13 20:12

Trident D'Gao


3 Answers

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.

like image 58
pad Avatar answered Nov 11 '22 11:11

pad


I think

failwith "Not implemented"

would be pretty much equivalent

like image 31
John Palmer Avatar answered Nov 11 '22 10:11

John Palmer


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.

like image 5
V.B. Avatar answered Nov 11 '22 11:11

V.B.