我有以下脚本。Map.tryFind返回null而不是无
type SetCookie = {
NameValue : string * string;
Domain : string option;
Path : string option;
Expires : string option;
MaxAge : string option;
Secure : bool; // ? no associated value, anything better than bool
HttpOnly : bool; // ? no associated value, anything better than bool
}
let GetCookie str =
let allkeyvalues =
str.Split ';'
|> Array.map (fun str ->
match str.Split '=' with
| [| key |] -> (key.Trim(), "")
| [| key; value |] -> (key.Trim(), value)
| _ -> failwith "there's more than one '='; the format is incorrect.")
let namevalue = allkeyvalues.[0]
let attributes =
Seq.skip 1 allkeyvalues
|> Seq.map (fun (key, value) ->
(key.ToLower(), value)) // attribute names are case-insensitive
|> Map.ofSeq
{
NameValue = namevalue
Domain = Map.tryFind "domain" attributes
Path = Map.tryFind "path" attributes
Expires = Map.tryFind "expires" attributes
MaxAge = Map.tryFind "maxage" attributes
Secure = Map.containsKey "secure" attributes
HttpOnly = Map.containsKey "httponly" attributes
}
然而,函数调用
GetCookie "XXX=YYY; path=/"
返回null Domain
,Expires
....和所有其他选项类型,而不是无。根据文件,如果没有找到它应该返回None。 http://msdn.microsoft.com/en-us/library/ee353538.aspx
{NameValue =
("XXX", "YYY");
Domain = null;
Path = Some "/";
Expires = null;
MaxAge = null;
Secure = false;
HttpOnly = false;}