Result handing in D: some|none

SealabJaster sealabjaster at gmail.com
Sun Jan 30 16:13:48 UTC 2022


On Sunday, 30 January 2022 at 16:08:17 UTC, SealabJaster wrote:
> On Sunday, 30 January 2022 at 10:58:33 UTC, 9il wrote:
>> ...
> ...
I missed the full example I wanted to give:

```fsharp
let (|Seconds|NotANumber|) (str : string) =
     let mutable value = 0.0
     match Double.TryParse(str, &value) with
     | true -> Seconds value
     | false -> NotANumber

let (|ValidRequest|InvalidRequest|) (model : PutSubModel, rs : 
ResultSet) =
     if model.Taken > DateTimeOffset.UtcNow then
         InvalidRequest "This result was taken in the future?"
     ...
     else
         match model.Duration with
         | Seconds s -> ValidRequest {
                ...
             }
         | NotANumber -> InvalidRequest "Duration is not a valid 
number"

let rec handleModel (model: PutSubModel) (array: PutSubModel[]) =
     match (model, rs) with
     | InvalidRequest error -> Some error
     | ValidRequest req ->
         req |> db.Add |> ignore
         if array.Length = 0 then
             None
         else
             handleModel array.[0] array.[1..]
```



More information about the Digitalmars-d-announce mailing list