Sorry if this is a dumb question, but I'm struggling with Language-Ext, and can't seem to find a neat way to do this.
In non-functional code, I could do something like this...
async Task DoFerretStuff(string id) {
Ferret? ferret = await ctx.Ferrets.FirstOrDefaultAsync(f => f.id == id);
if (ferret == null) {
// Do whatever needs doing when we can't find the ferret
} else {
// Do ferret stuff
}
}
I'm trying to do this in a more functional way, and assumed I would be able to do something like this...
async Task<Unit> DoFerretStuff(string id) =>
new Option<Ferret>(await ctx.Ferrets.FirstOrDefaultAsync(f => f.Id == id))
.Match(ferret => {
// Do ferret stuff
return unit;
},
() => {
// Do whatever needs doing when we can't find the ferret
return unit;
});
However, this gives a compiler error on the first line...
cannot convert from 'Ferret' to 'System.Collections.Generic.IEnumerable'
I don't understand why, as I thought the idea was that you could pass a (possibly null) object into the constructor for Option<T>
and it would give either a Some<T>
or a None<T>
.
Please can someone explain how I should do this sort of thing in a functional way.
Thanks