Search code examples
f#initializationoption-typemutable

How to initialize record with option types in fsharp


do you know how I can initialize the variable ret below ?

   type ReferenceDataResponse = 
      { ResponseError : ResponseError option
        SecurityDatas : SecurityData array option }


   let ToReferenceDataResponse(elem:Bloomberglp.Blpapi.Element) =
      let ret =  { ResponseError = null ; SecurityDatas = null }

      if elem.HasElement("ResponseError") then
         ...
      end

ps : I imagine I have somehow to declare my record values mutable

UPDATE :

As mentioned in the comment, I had a previous usage of None before in my code :

 type ZeroOrMany<'a> = 
     | Many of 'a array 
     | None

That created a new definition for the symbol 'None' which was hiding the "Option.None" I was trying to refer to here.


Solution

  • You want None instead of null.

    As for updating fields, you can mark them mutable or use copy-and-update expressions: let newRet = { ret with ResponseError = Some(error) }. Which one you use depends on if you need persistence.