Error using bool.Parse on null/empty values

眉间皱痕 提交于 2019-12-06 06:06:56
Anton Schwaighofer

There's quite a few places where you can safeguard via pattern matching: dictionary lookup, casting, parsing. Here's an example with all of those:

let allDayEvent (d: kv) = 
    match d.TryGetValue "fAllDayEvent" with
    | true, v ->
        match v with
        | null -> printfn "null found"
        | :? string as s -> 
            match bool.TryParse s with
            | true, b -> printfn "found a bool: %A" b
            | _ -> printfn "That's not a bool?"
        | v -> printfn "Found something of type %s" (v.GetType().Name)
    | _ -> printfn "No such key"

See also related questions, for example this.

Not sure why you are using a Dictionary, but I would probably have gone for a Map instead. Or at least done some Conversion to Map somewhere. And then I would maybe have thrown in some "automagically" handling of nulls.

And then Pandoras Box is kind of opened, but....

let (|Bool|) str =
   match System.Boolean.TryParse(str) with
   | (true,bool) -> Some(bool)
   | _ -> None   

let (|String|) (o:obj) = 
    match o with
    | :? string as s -> Some(s)
    | _ -> None 

type kv = Dictionary<string, obj>

let allDayEvent (d: kv) = 
    d :> seq<_>
    |> Seq.map (|KeyValue|)
    |> Map.ofSeq 
    |> Map.tryFind "fAllDayEvent" 
    |> Option.bind (|String|)
    |> Option.bind  (|Bool|)

Note that allDayEvent in the above now is an Option, which maybe is in fact what you need/want.

And it does keep all data in place. Like true or false is not the same as "did not find stuff" or "could not convert stuff to some bool". Now it is in fact one of the following:

  1. key found and some string like "true": Some(true)
  2. key found and some string like "false": Some(false)
  3. key not found or string not convertable to bool: None

Code is not tested and may need some further massaging.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!