6 ms·
if(!Whatever.TryParse<Thingy>(input, out var output)) output = some-sane-default; I absolutely hate that. IMO you should handle the error of an invalid input o
by Akronymus 2y ago
if(!Whatever.TryParse<Thingy>(input, out var output)) output = some-sane-default;
I absolutely hate that. IMO you should handle the error of an invalid input outside of the function to parse. F# makes that easy.
type Whatever =
static member create input =
match input with
| ValidWhatever x -> Some x
| _ -> None
match Whatever.create input with
| Some x -> //process the parsed data
| None -> //handle it not being parsed well
Or you could also use Option.map/Option.bind to build a pipeline to handle chained operations in an ergonomic manner.
With this, you can only instantiate any instances through the create method with parses the input.
Altough, you probably want to use a result rather than option, but I digress.