language-ext icon indicating copy to clipboard operation
language-ext copied to clipboard

Suggestion: MapT async overload for Task<Option>

Open MrWuffels opened this issue 2 years ago • 3 comments

Discussed in https://github.com/louthy/language-ext/discussions/1013

Originally posted by MrWuffels March 25, 2022 Hi,

I'm new here and I don't know if this was suggested or discussed already, at least I didn't find anything.

I came across following "issue":

Func<string,Task<Option<string>>> bindTTest = str => Task.FromResult(Some("Haha"));
Func<string, Task<string>> mapTTest = str => Task.FromResult("haha");

var taskOption = GetTaskOption(); //returns Task<Option<string>>
var bindT = taskOption.BindT(x => bindTTest(x)); //returns Task<Option<string>>
var mapT = taskOption.MapT(x => mapTTest(x)); //returns Task<Option<Task<string>>>

As you can see in this little example, BindT returns a Task<Option> as expected. MapT however returns Task<Option<Task>>, but I would expect Task<Option> like it does in BindT. The difference is while BindT has an overload where it expects a Func with a Task as return value, MapT doesn't seem to be prepared for it. My suggestion would be adding a similar overload to MapT. Or do I miss something?

Of course I have read way to late that suggestions should be made on the issues page. Shame on me. However here we are, still the same "problem". So, maybe you have a few thoughts about this idea?

MrWuffels avatar Apr 01 '22 11:04 MrWuffels

@MrWuffels The extension methods are all code-gen'd, which is why these anomalies happen (with Task extensions). It's a bit of a tangled mess, so I'd prefer to leave this until I replace the T4 templates with something smarter. I'd suggest adding an extension of your own that does what you need, or using OptionAsync which is a type that captures optional-tasks.

louthy avatar Apr 06 '22 12:04 louthy

Thank you for the answer. Then I'm probably going to follow your advise and just add a similar method by my own, but of course I'm looking forward to the replacement ;)

MrWuffels avatar Apr 08 '22 08:04 MrWuffels

@louthy I took a quick look into your solution, because we just need the extension for a few monads. So I would like to ask if you'd allow me to add at least the few extensions we need and create a pull request, that would make our lives a bit easier. Of course I understand if this makes even less sense when this extensions only exist for a few ones, and when you like to replace the whole thing sooner or later anyway

MrWuffels avatar Apr 08 '22 11:04 MrWuffels