Skip to content

Commit 383f292

Browse files
cheesengmbovel
authored andcommitted
Enable sc:compile for stdlib package scala.concurrent (scala#25867)
Added sc:compile to scala source files under scala.concurrent package.
1 parent 5362f32 commit 383f292

8 files changed

Lines changed: 145 additions & 95 deletions

File tree

library/src/scala/concurrent/BatchingExecutor.scala

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,38 @@ private[concurrent] object BatchingExecutorStatics {
6060
* When you implement this trait for async executors like thread pools,
6161
* you're going to need to implement it something like the following:
6262
*
63-
* ```
64-
* final override def submitAsync(runnable: Runnable): Unit =
65-
* super[SuperClass].execute(runnable) // To prevent reentrancy into `execute`
63+
* ```scala sc:compile
64+
* import java.util.concurrent.Executor
65+
*
66+
* final class AsyncBatchingExecutor(delegate: Executor)
67+
* extends ExecutionContextExecutor
68+
* with BatchingExecutor {
69+
* final override def submitForExecution(runnable: Runnable): Unit =
70+
* delegate.execute(runnable)
6671
*
67-
* final override def execute(runnable: Runnable): Unit =
68-
* if (runnable.isInstanceOf[Batchable]) // Or other logic
69-
* submitAsyncBatched(runnable)
70-
* else
71-
* submitAsync(runnable)
72+
* final override def execute(runnable: Runnable): Unit =
73+
* if (runnable.isInstanceOf[Batchable])
74+
* submitAsyncBatched(runnable)
75+
* else
76+
* submitForExecution(runnable)
7277
*
73-
* final override def reportFailure(cause: Throwable): Unit = …
78+
* final override def reportFailure(cause: Throwable): Unit = ()
79+
* }
7480
* ```
7581
*
7682
* And if you want to implement if for a sync, trampolining, executor you're
7783
* going to implement it something like this:
7884
*
79-
* ```
80-
* final override def submitAsync(runnable: Runnable): Unit = ()
85+
* ```scala sc:compile
86+
* final class TrampoliningExecutor extends ExecutionContextExecutor with BatchingExecutor {
87+
* final override def submitForExecution(runnable: Runnable): Unit = ()
8188
*
82-
* final override def execute(runnable: Runnable): Unit =
83-
* submitSyncBatched(runnable) // You typically will want to batch everything
89+
* final override def execute(runnable: Runnable): Unit =
90+
* submitSyncBatched(runnable)
8491
*
85-
* final override def reportFailure(cause: Throwable): Unit =
86-
* ExecutionContext.defaultReporter(cause) // Or choose something more fitting
92+
* final override def reportFailure(cause: Throwable): Unit =
93+
* ExecutionContext.defaultReporter(cause)
94+
* }
8795
* ```
8896
*/
8997
private[concurrent] trait BatchingExecutor extends Executor {

library/src/scala/concurrent/BlockContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import scala.language.`2.13`
2727
*
2828
* Typically, you'll want to chain to the previous `BlockContext`,
2929
* like this:
30-
* ```
30+
* ```scala sc:compile
3131
* val oldContext = BlockContext.current
3232
* val myContext = new BlockContext {
3333
* override def blockOn[T](thunk: => T)(implicit permission: CanAwait): T = {

library/src/scala/concurrent/ExecutionContext.scala

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ object ExecutionContext {
155155
* in case the `opportunistic` field is missing (example below). The resulting `ExecutionContext` has batching
156156
* behavior in all Scala 2.13 versions (`global` is batching in 2.13.0-3).
157157
*
158-
* ```
158+
* ```scala sc:compile
159159
* implicit val ec: scala.concurrent.ExecutionContext = try {
160160
* scala.concurrent.ExecutionContext.getClass
161161
* .getDeclaredMethod("opportunistic")
@@ -174,19 +174,23 @@ object ExecutionContext {
174174
* 1. Writing a Scala `object` in the `scala` package (example below).
175175
* 1. Writing a Java source file. This works because `private[scala]` is emitted as `public` in Java bytecode.
176176
*
177-
* ```
178-
* // Option 1
177+
* ```scala sc:compile
178+
* import scala.language.reflectiveCalls
179+
*
180+
* type ExecutionContextCompanionApi = AnyRef {
181+
* def opportunistic: scala.concurrent.ExecutionContextExecutor
182+
* }
183+
*
179184
* implicit val ec: scala.concurrent.ExecutionContext =
180-
* (scala.concurrent.ExecutionContext:
181-
* {def opportunistic: scala.concurrent.ExecutionContextExecutor}
182-
* ).opportunistic
183-
*
184-
* // Option 2
185-
* package scala {
186-
* object OpportunisticEC {
187-
* implicit val ec: scala.concurrent.ExecutionContext =
188-
* scala.concurrent.ExecutionContext.opportunistic
189-
* }
185+
* scala.concurrent.ExecutionContext
186+
* .asInstanceOf[ExecutionContextCompanionApi]
187+
* .opportunistic
188+
* ```
189+
*
190+
* ```scala sc:compile
191+
* object OpportunisticEC {
192+
* implicit val ec: scala.concurrent.ExecutionContext =
193+
* scala.concurrent.ExecutionContext.opportunistic
190194
* }
191195
* ```
192196
*
@@ -252,7 +256,7 @@ object ExecutionContext {
252256
* If it is guaranteed that none of the executed tasks are blocking, a single-threaded `ExecutorService`
253257
* can be used to create an `ExecutionContext` as follows:
254258
*
255-
* ```
259+
* ```scala sc:compile
256260
* import java.util.concurrent.Executors
257261
* val ec = ExecutionContext.fromExecutorService(Executors.newSingleThreadExecutor())
258262
* ```

library/src/scala/concurrent/Future.scala

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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`

library/src/scala/concurrent/duration/Deadline.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import scala.language.`2.13`
1717
/** This class stores a deadline, as obtained via `Deadline.now` or the
1818
* duration DSL:
1919
*
20-
* ```
21-
* import scala.concurrent.duration._
20+
* ```scala sc:compile
21+
* import scala.concurrent.duration.*
2222
* 3.seconds.fromNow
2323
* ```
2424
*

library/src/scala/concurrent/duration/Duration.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,11 @@ object Duration {
323323
*
324324
* <p/>
325325
* Examples:
326-
* ```
327-
* import scala.concurrent.duration._
326+
* ```scala sc:compile
327+
* import scala.concurrent.duration.*
328328
*
329329
* val duration = Duration(100, MILLISECONDS)
330-
* val duration = Duration(100, "millis")
330+
* val sameDuration = Duration(100, "millis")
331331
*
332332
* duration.toNanos
333333
* duration < 1.second
@@ -338,16 +338,18 @@ object Duration {
338338
*
339339
* <p/>
340340
* Implicits are also provided for Int, Long and Double. Example usage:
341-
* ```
342-
* import scala.concurrent.duration._
341+
* ```scala sc:compile
342+
* import scala.concurrent.duration.*
343343
*
344344
* val duration = 100.millis
345345
* ```
346346
*
347347
* ***The DSL provided by the implicit conversions always allows construction of finite durations, even for infinite Double inputs; use Duration.Inf instead.***
348348
*
349349
* Extractors, parsing and arithmetic are also included:
350-
* ```
350+
* ```scala sc:compile
351+
* import scala.concurrent.duration.*
352+
* import scala.language.postfixOps
351353
* val d = Duration("1.2 µs")
352354
* val Duration(length, unit) = 5 millis
353355
* val d2 = d * 2.5
@@ -567,7 +569,8 @@ sealed abstract class Duration extends Serializable with Ordered[Duration] {
567569
/** Returns duration which is equal to this duration but with a coarsest Unit, or self in case it is already the coarsest Unit
568570
* <p/>
569571
* Examples:
570-
* ```
572+
* ```scala sc:compile
573+
* import scala.concurrent.duration.*
571574
* Duration(60, MINUTES).toCoarsest // Duration(1, HOURS)
572575
* Duration(1000, MILLISECONDS).toCoarsest // Duration(1, SECONDS)
573576
* Duration(48, HOURS).toCoarsest // Duration(2, DAYS)

0 commit comments

Comments
 (0)