我有以下脚本.
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
}
Run Code Online (Sandbox Code Playgroud)
但是,函数调用
GetCookie "XXX=YYY; path=/"
Run Code Online (Sandbox Code Playgroud)
返回null for Domain,Expires....和所有其他选项类型而不是None.根据文档,如果没有找到它应该返回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;}
Run Code Online (Sandbox Code Playgroud)
F#None值实际上由.NET表示null.在这种情况下,这会以通用方式打印出来,但在其他方面只是一个表示细节.实际上,您可以通过注释没有参数的联合案例来为您自己的歧视联盟获取此行为CompilationRepresentationFlags.UseNullAsTrueValue.