@@ -31,7 +31,7 @@ import scala.concurrent.impl.Promise.DefaultPromise
3131 * Computations are executed using an `ExecutionContext`, which is usually supplied implicitly,
3232 * and which is commonly backed by a thread pool.
3333 *
34- * ```
34+ * ```scala sc:compile
3535 * import ExecutionContext.Implicits.global
3636 * val s = "Hello"
3737 * val f: Future[String] = Future {
@@ -79,7 +79,8 @@ import scala.concurrent.impl.Promise.DefaultPromise
7979 * @define forComprehensionExamples
8080 * Example:
8181 *
82- * ```
82+ * ```scala sc:compile
83+ * import ExecutionContext.Implicits.global
8384 * val f = Future { 5 }
8485 * val g = Future { 3 }
8586 * val h = for {
@@ -90,7 +91,10 @@ import scala.concurrent.impl.Promise.DefaultPromise
9091 *
9192 * is translated to:
9293 *
93- * ```
94+ * ```scala sc:compile
95+ * import ExecutionContext.Implicits.global
96+ * val f = Future { 5 }
97+ * val g = Future { 3 }
9498 * f flatMap { (x: Int) => g map { (y: Int) => x + y } }
9599 * ```
96100 *
@@ -237,9 +241,10 @@ trait Future[+T] extends Awaitable[T] {
237241 *
238242 * Example:
239243 *
240- * ```
244+ * ```scala sc:compile
245+ * import ExecutionContext.Implicits.global
241246 * val f = Future { "The future" }
242- * val g = f map { x: String => x + " is now!" }
247+ * val g = f. map { ( x: String) => x + " is now!" }
243248 * ```
244249 *
245250 * Note that a for comprehension involving a `Future`
@@ -291,7 +296,9 @@ trait Future[+T] extends Awaitable[T] {
291296 * If the current future fails, then the resulting future also fails.
292297 *
293298 * Example:
294- * ```
299+ * ```scala sc:compile
300+ * import ExecutionContext.Implicits.global
301+ * import scala.concurrent.duration.Duration
295302 * val f = Future { 5 }
296303 * val g = f filter { _ % 2 == 1 }
297304 * val h = f filter { _ % 2 == 0 }
@@ -329,7 +336,9 @@ trait Future[+T] extends Awaitable[T] {
329336 * If the current future fails, then the resulting future also fails.
330337 *
331338 * Example:
332- * ```
339+ * ```scala sc:compile
340+ * import ExecutionContext.Implicits.global
341+ * import scala.concurrent.duration.Duration
333342 * val f = Future { -5 }
334343 * val g = f collect {
335344 * case x if x < 0 => -x
@@ -361,10 +370,11 @@ trait Future[+T] extends Awaitable[T] {
361370 *
362371 * Example:
363372 *
364- * ```
365- * Future (6 / 0) recover { case e: ArithmeticException => 0 } // result: 0
366- * Future (6 / 0) recover { case e: NotFoundException => 0 } // result: exception
367- * Future (6 / 2) recover { case e: ArithmeticException => 0 } // result: 3
373+ * ```scala sc:compile
374+ * import ExecutionContext.Implicits.global
375+ * Future(6 / 0).recover { case _: ArithmeticException => 0 } // result: 0
376+ * Future(6 / 0).recover { case _: NoSuchElementException => 0 } // result: exception
377+ * Future(6 / 2).recover { case _: ArithmeticException => 0 } // result: 3
368378 * ```
369379 *
370380 * @tparam U the type of the returned `Future`
@@ -384,9 +394,10 @@ trait Future[+T] extends Awaitable[T] {
384394 *
385395 * Example:
386396 *
387- * ```
397+ * ```scala sc:compile
398+ * import ExecutionContext.Implicits.global
388399 * val f = Future { Int.MaxValue }
389- * Future (6 / 0) recoverWith { case e : ArithmeticException => f } // result: Int.MaxValue
400+ * Future(6 / 0). recoverWith { case _ : ArithmeticException => f } // result: Int.MaxValue
390401 * ```
391402 *
392403 * @tparam U the type of the returned `Future`
@@ -454,7 +465,8 @@ trait Future[+T] extends Awaitable[T] {
454465 * Using this method will not cause concurrent programs to become nondeterministic.
455466 *
456467 * Example:
457- * ```
468+ * ```scala sc:compile
469+ * import ExecutionContext.Implicits.global
458470 * val f = Future { throw new RuntimeException("failed") }
459471 * val g = Future { 5 }
460472 * val h = f fallbackTo g
@@ -508,7 +520,9 @@ trait Future[+T] extends Awaitable[T] {
508520 *
509521 * The following example prints out `5`:
510522 *
511- * ```
523+ * ```scala sc:compile
524+ * import ExecutionContext.Implicits.global
525+ * import scala.util.{Failure, Success}
512526 * val f = Future { 5 }
513527 * f andThen {
514528 * case r => throw new RuntimeException("runtime exception")
@@ -689,10 +703,12 @@ object Future {
689703 *
690704 * The following expressions are equivalent:
691705 *
692- * ```
693- * val f1 = Future(expr)
694- * val f2 = Future.unit.map(_ => expr)
695- * val f3 = Future.unit.transform(_ => Success(expr))
706+ * ```scala sc:compile
707+ * import ExecutionContext.Implicits.global
708+ * import scala.util.Success
709+ * val f1 = Future(1 + 1)
710+ * val f2 = Future.unit.map(_ => 1 + 1)
711+ * val f3 = Future.unit.transform(_ => Success(1 + 1))
696712 * ```
697713 *
698714 * The result becomes available once the asynchronous computation is completed.
@@ -709,10 +725,11 @@ object Future {
709725 *
710726 * The following expressions are semantically equivalent:
711727 *
712- * ```
713- * val f1 = Future(expr).flatten
714- * val f2 = Future.delegate(expr)
715- * val f3 = Future.unit.flatMap(_ => expr)
728+ * ```scala sc:compile
729+ * import ExecutionContext.Implicits.global
730+ * val f1 = Future(Future.successful(1 + 1)).flatten
731+ * val f2 = Future.delegate(Future.successful(1 + 1))
732+ * val f3 = Future.unit.flatMap(_ => Future.successful(1 + 1))
716733 * ```
717734 *
718735 * The result becomes available once the resulting Future of the asynchronous computation is completed.
@@ -805,8 +822,10 @@ object Future {
805822 * or the result of the fold.
806823 *
807824 * Example:
808- * ```
809- * val futureSum = Future.foldLeft(futures)(0)(_ + _)
825+ * ```scala sc:compile
826+ * import ExecutionContext.Implicits.global
827+ * val futures = List(Future.successful(1), Future.successful(2), Future.successful(3))
828+ * val futureSum = Future.foldLeft(futures)(0)(_ + _)
810829 * ```
811830 *
812831 * @tparam T the type of the value of the input Futures
@@ -829,8 +848,10 @@ object Future {
829848 * or the result of the fold.
830849 *
831850 * Example:
832- * ```
833- * val futureSum = Future.fold(futures)(0)(_ + _)
851+ * ```scala sc:compile
852+ * import ExecutionContext.Implicits.global
853+ * val futures = List(Future.successful(1), Future.successful(2), Future.successful(3))
854+ * val futureSum = Future.fold(futures)(0)(_ + _)
834855 * ```
835856 *
836857 * @tparam T the type of the value of the input Futures
@@ -850,8 +871,10 @@ object Future {
850871 * where the fold-zero is the result value of the first `Future` in the collection.
851872 *
852873 * Example:
853- * ```
854- * val futureSum = Future.reduce(futures)(_ + _)
874+ * ```scala sc:compile
875+ * import ExecutionContext.Implicits.global
876+ * val futures = List(Future.successful(1), Future.successful(2), Future.successful(3))
877+ * val futureSum = Future.reduce(futures)(_ + _)
855878 * ```
856879 * @tparam T the type of the value of the input Futures
857880 * @tparam R the type of the value of the returned `Future`
@@ -869,8 +892,10 @@ object Future {
869892 * where the zero is the result value of the first `Future`.
870893 *
871894 * Example:
872- * ```
873- * val futureSum = Future.reduceLeft(futures)(_ + _)
895+ * ```scala sc:compile
896+ * import ExecutionContext.Implicits.global
897+ * val futures = List(Future.successful(1), Future.successful(2), Future.successful(3))
898+ * val futureSum = Future.reduceLeft(futures)(_ + _)
874899 * ```
875900 * @tparam T the type of the value of the input Futures
876901 * @tparam R the type of the value of the returned `Future`
@@ -889,8 +914,11 @@ object Future {
889914 * This is useful for performing a parallel map. For example, to apply a function to all items of a list
890915 * in parallel:
891916 *
892- * ```
893- * val myFutureList = Future.traverse(myList)(x => Future(myFunc(x)))
917+ * ```scala sc:compile
918+ * import ExecutionContext.Implicits.global
919+ * val myList = List(1, 2, 3)
920+ * def myFunc(x: Int): Int = x + 1
921+ * val myFutureList = Future.traverse(myList)(x => Future(myFunc(x)))
894922 * ```
895923 * @tparam A the type of the value inside the Futures in the collection
896924 * @tparam B the type of the value of the returned `Future`
0 commit comments