Skip to content

Commit 10f3c8d

Browse files
committed
Minimize the public API
- Remove the CPS fallback version of async. That was not intended to be part of 1.0. - Lookup the await method beside the macro, rather than requiring all calls to go to AsyncBase.await. - Create a minimal version of Async that just contains await/async and delegates to the macro implementation in internal._ - Add scaladoc.
1 parent 6a2b940 commit 10f3c8d

18 files changed

+74
-420
lines changed

build.sbt

+1-22
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,6 @@ testOptions += Tests.Argument(TestFrameworks.JUnit, "-q", "-v", "-s")
2828

2929
parallelExecution in Global := false
3030

31-
autoCompilerPlugins := true
32-
33-
scalacOptions ++= (scalaHome.value match {
34-
case Some(sh) =>
35-
// Use continuations plugin from the local scala instance
36-
val continuationsJar = sh / "misc" / "scala-devel" / "plugins" / "continuations.jar"
37-
("-Xplugin:" + continuationsJar.getAbsolutePath) :: Nil
38-
case None =>
39-
Nil
40-
})
41-
42-
libraryDependencies ++= (scalaHome.value match {
43-
case Some(sh) =>
44-
Nil
45-
case None =>
46-
// Use continuations plugin from the published artifact.
47-
compilerPlugin("org.scala-lang.plugins" % "continuations" % scalaVersion.value) :: Nil
48-
})
49-
50-
scalacOptions += "-P:continuations:enable"
51-
5231
scalacOptions in compile ++= Seq("-optimize", "-deprecation", "-unchecked", "-Xlint", "-feature")
5332

5433
scalacOptions in Test ++= Seq("-Yrangepos")
@@ -133,4 +112,4 @@ packageOptions in packageSrc := Seq(Package.ManifestAttributes(
133112
("Bundle-Name", s"${name.value} sources"),
134113
("Bundle-Version", osgiVersion.value),
135114
("Eclipse-SourceBundle", s"""${organization.value}.${name.value};version="${osgiVersion.value}";roots:="."""")
136-
))
115+
))

pending/run/fallback0/fallback0-manual.scala

-71
This file was deleted.

pending/run/fallback0/fallback0.scala

-48
This file was deleted.
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com>
3+
*/
4+
5+
package scala.async
6+
7+
import scala.language.experimental.macros
8+
import scala.concurrent.{Future, ExecutionContext}
9+
import scala.reflect.internal.annotations.compileTimeOnly
10+
11+
/**
12+
* Async blocks provide a direct means to work with [[scala.concurrent.Future]].
13+
*
14+
* For example, to use an API to that fetches as web page to fetch
15+
* two pages and add their lengths:
16+
*
17+
* {{{
18+
* import ExecutionContext.Implicits.global
19+
* import scala.async.Async.{async, await}
20+
*
21+
* def fetchURL(url: URL): Future[String] = ...
22+
*
23+
* val sumLengths: Future[Int] = async {
24+
* val body1 = fetchURL("http://scala-lang.org")
25+
* val body2 = fetchURL("http://docs.scala-lang.org")
26+
* await(body1).length + await(body2).length
27+
* }
28+
* }}}
29+
*
30+
* Note that the in the following program, the second fetch does *not* start
31+
* until after the first. If you need to start tasks in parallel, you must do
32+
* so before `await`-ing a result.
33+
*
34+
* {{{
35+
* val sumLengths: Future[Int] = async {
36+
* await(fetchURL("http://scala-lang.org")).length + await(fetchURL("http://docs.scala-lang.org")).length
37+
* }
38+
* }}}
39+
*/
40+
object Async {
41+
/**
42+
* Run the block of code `body` asynchronously. `body` may contain calls to `await` when the results of
43+
* a `Future` are needed; this is translated into non-blocking code.
44+
*/
45+
def async[T](body: T)(implicit execContext: ExecutionContext): Future[T] = macro internal.ScalaConcurrentAsync.asyncImpl[T]
46+
47+
/**
48+
* Non-blocking await the on result of `awaitable`. This may only be used directly within an enclosing `await` block.
49+
*
50+
* Internally, this will register the remainder of the code in enclosing `async` block as a callback
51+
* in the `onComplete` handler of `awaitable`, and will *not* block a thread.
52+
*/
53+
@compileTimeOnly("`await` must be enclosed in an `async` block")
54+
def await[T](awaitable: Future[T]): T = ??? // No implementation here, as calls to this are translated to `onComplete` by the macro.
55+
}

src/main/scala/scala/async/StateMachine.scala

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package scala.async
66

77
/** Internal class used by the `async` macro; should not be manually extended by client code */
8+
// NOTE: this is not in the `internal` package as we must keep this binary compatible as it extended
9+
// by the translated code.
810
abstract class StateMachine[Result, EC] extends (scala.util.Try[Any] => Unit) with (() => Unit) {
911
def result: Result
1012

src/main/scala/scala/async/continuations/AsyncBaseWithCPSFallback.scala

-102
This file was deleted.

src/main/scala/scala/async/continuations/AsyncWithCPSFallback.scala

-25
This file was deleted.

src/main/scala/scala/async/continuations/CPSBasedAsync.scala

-24
This file was deleted.

src/main/scala/scala/async/continuations/CPSBasedAsyncBase.scala

-23
This file was deleted.

0 commit comments

Comments
 (0)