Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# how to return have value a tuple or null

Tags:

f#

    let retVal =
      if reader.Read() then
        (reader.GetString(0), getBytesData reader 1, reader.GetDateTime(2))
      else
        null

F# don't allow null to returned

How can i have value return as a tuple or a null?

like image 367
mamu Avatar asked Jun 06 '10 05:06

mamu


1 Answers

It is not that F# does not allow you to return null.

It is because then part and else part have different types.

You can use Option type.

let retVal =
  if reader.Read() then
    Some (reader.GetString(0), getBytesData reader 1, reader.GetDateTime(2))
  else
    None

when you use retVal, you use pattern matching:

match retVal with
| Some v -> ...
| None -> // null case
like image 79
Yin Zhu Avatar answered Nov 15 '22 09:11

Yin Zhu