Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function for creating a Task from a value #554

Merged
merged 1 commit into from
Oct 13, 2023
Merged

Conversation

gusty
Copy link
Member

@gusty gusty commented Sep 19, 2023

We normally uses Task.FromValue but nowadays, using the task CE the namespace System.Threading.Tasks is not always opened and opening it only to return a task doesn't feel so good.

Sometimes what I do is I use result but in some places is too generic.

Another option is writing task { return x } but I think is overkill.

Given that we have our Task module in scope with many functions, maybe natural way is to add one there like List.singleton does.

Copy link
Member

@wallymathieu wallymathieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@Happypig375
Copy link
Contributor

Why not Task.singleton?

@gusty
Copy link
Member Author

gusty commented Sep 21, 2023

Thanks, the idea is to get some feedback about names first, before merging this.
If you ask me, it looks like singleton is used in list-like collections, as opposed to having multiple values, you have a singleton one.

@abelbraaksma
Copy link
Member

I agree that this function should be added, maybe also for ValueTask and Async? I think that @Happypig375 refers to the use of Task.singleton and the like from FsToolkit (see https://github.com/search?q=repo%3Ademystifyfp%2FFsToolkit.ErrorHandling%20singleton&type=code).

But it feels wrong, 'singleton' suggests that there can also be more. I've seen, in the wild, Task.ret, Task.retnwhich may be a good alias, but I think most people would naturally search for something that starts withof, like Task.ofAsync, Task.ofUnit(from non generic tasks),Task.ofValueTask` etc.

But keep in mind that there was an effort underway where we wanted a very thin library for these super trivial extensions for F#, because these trivial ones seem to be created and exposed in a bunch of libraries, often with different names.

The thin lib could be source-embedded. I forgot the most recent state of that discussion, I'll look it up.

@gusty gusty force-pushed the master branch 2 times, most recently from c718153 to 3962979 Compare October 8, 2023 07:27
@gusty gusty merged commit d35fb08 into master Oct 13, 2023
6 checks passed
@gusty
Copy link
Member Author

gusty commented Oct 13, 2023

I like Task.result or Task.ofResult as it matches the generic result function and also the .FromResult BCL method.

The problem is, what if in the future we decide to add an .ofResult which takes a Result value and make it a Task with either a value or an exception (failed task) ?

Keeping that in mind I still consider Task.result (without 'of') as a valid candidate, then we can add Task.fail / Task.failwith

@gusty
Copy link
Member Author

gusty commented Oct 13, 2023

This PR was already merged my mistake.

I'm feeling that Task.result should be the name of the function, for reasons explained above.

Against .ofValue I have to say that there is no precedent of such name here nor in FSharp.Core.

Is anyone strongly opposed to Task.result? If so please shout.

@gusty
Copy link
Member Author

gusty commented Oct 14, 2023

Both Task.result and ValueTask.result added at 21f4a0b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants