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

Introduce an initial Option for async computation expressions #808

Closed
giuliohome opened this issue Nov 21, 2019 · 3 comments
Closed

Introduce an initial Option for async computation expressions #808

giuliohome opened this issue Nov 21, 2019 · 3 comments
Labels

Comments

@giuliohome
Copy link

@giuliohome giuliohome commented Nov 21, 2019

An Option transformer for async functions

I propose we have a new member for Option so that we can mark an async function as returned by the server, instead of being an initial value.

The existing way of approaching this problem in F# is to write a type extension for Option

 type Option<'a> with
     static member toSomeAsync<'a> (f: unit -> Async<'a>) 
         : unit -> Async<'a option>  = 
         fun () -> 
             async {
                 let! (res : 'a) = f ()
                 return Some res
             }

Pros and Cons

The advantages of making this adjustment to F# are:

  1. a standard way of wrapping the Option inside an async workflow (it would vaguely look like the equivalent of a monad transformer)
  2. a quick way to mark an async function returning a computed value as distinct from an initial, non computed value

I'm not aware of any disadvantages of making this adjustment to F#.

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this
@Happypig375

This comment has been minimized.

Copy link
Contributor

@Happypig375 Happypig375 commented Nov 22, 2019

This should really be Async.toOption.

@dsyme

This comment has been minimized.

Copy link
Collaborator

@dsyme dsyme commented Nov 22, 2019

If we do this we would add Async.map and Async.toOption:

module Async = 
     let map f (computation: Async<'a>) =
             async {
                 let! res = computation
                 return f res
             }

     let toOption computation = map Some computation

The unit play no role since Async are delayed and started explicitly, not implicitly

@cartermp

This comment has been minimized.

Copy link
Member

@cartermp cartermp commented Nov 23, 2019

@giuliohome in this case I think the resolution would be that you write and use your own library function.

@giuliohome giuliohome closed this Nov 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.