develooper Front page | perl.perl5.porters | Postings from July 2019

I would love to write: async sub + signatures

Thread Next
From:
Paul "LeoNerd" Evans
Date:
July 3, 2019 11:36
Subject:
I would love to write: async sub + signatures
Message ID:
20190703123559.6231ff71@shy.leonerd.org.uk
((A followup to my message
  Subject: Metathread - Programs I would love to write
))

I would love to write

  use feature 'signatures';
  use Future::AsyncAwait;
  use Future::HTTP;

  async sub get_user_info($uid) {
    my $ua = Future::HTTP->new;
    my $body = await $ua->http_get("https://example.org/api/user/$uid");
    return json_decode($body->cobntent);
  }

This would be nice, as it combines two good features:

  * async/await syntax to neaten up asynchronous code and Future-based
    control flow. It looks a lot neater and more readable than the
    previous style of ->then chaining; a subject on which I have
    written at great length[1].

  * subroutine signatures to neaten up the unpacking of arguments
    passed into functions.

However, currently I cannot write this:

  Expected async sub NAME to be followed by '{' at ...

This is due to internal implementation reasons within
Future::AsyncAwait, but before I get into that I want first to
establish the overall theme for this thread; namely that:

  We believe the ability to combine `async sub` with signatures is a
  useful ability to try to achieve.

If we generally agree it'd be nice to be able to write this sort of
thing, then I'll expand more on why it currently doesn't work, why I
believe p5p are the place to begin solving it, and then we can work on
how to fix it.

(There has previously been some discussion on this issue, but it so far
 hasn't gone anywhere:

  https://rt.perl.org/Public/Bug/Display.html?id=132474
)

-----

[1]: http://leonerds-code.blogspot.com/2019/04/awaiting-future.html

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About