- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Proposal for helping out with concurrency
Mon, 2011-12-05, 14:34
People of the Scala language,
To prevent from unknowingly closing over non-threadsafe mutable state, I propose the following:
Adding @noAsync and @async, so you can tag that a closure/function parameter is @async, and then the compiler will barf if it closes over something annotated with @noAsync.
You may also declare a reference type as @async, which makes it eligible for closing over even if the parent is marked as @noAsync
@noAsync class Actor {
val self = ActorRef @async = ... //Means that it's OK to close over self
val foo = 2 //Since owning class is @noAsync, this is not legal to close over when closure is @async
}
class MyActor extends Actor {
def receive = {
case "foo" => Future { foo } //Illegal, since Future[T](thunk: T @async) requires what is closed over to be @async, or not have any parent that is @noAsync
}
}
This would also weed out accidental issues with ParallelCollections.
Ideas? Thoughts? Rants?
Cheers,
√
--
Viktor Klang
Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts
Twitter: @viktorklang
To prevent from unknowingly closing over non-threadsafe mutable state, I propose the following:
Adding @noAsync and @async, so you can tag that a closure/function parameter is @async, and then the compiler will barf if it closes over something annotated with @noAsync.
You may also declare a reference type as @async, which makes it eligible for closing over even if the parent is marked as @noAsync
@noAsync class Actor {
val self = ActorRef @async = ... //Means that it's OK to close over self
val foo = 2 //Since owning class is @noAsync, this is not legal to close over when closure is @async
}
class MyActor extends Actor {
def receive = {
case "foo" => Future { foo } //Illegal, since Future[T](thunk: T @async) requires what is closed over to be @async, or not have any parent that is @noAsync
}
}
This would also weed out accidental issues with ParallelCollections.
Ideas? Thoughts? Rants?
Cheers,
√
--
Viktor Klang
Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts
Twitter: @viktorklang
Mon, 2011-12-05, 14:47
#2
Re: Proposal for helping out with concurrency
2011/12/5 √iktor Ҡlang :
> Ideas? Thoughts? Rants?
As described, it should be extremely straightforward to implement as a
compiler plugin. Give it a try.
I'd be interested to see how feasible it would be to fully and
consistently annotate a large codebase such as Akka like this. I
strongly suspect that you'd very rapidly find yourself wanting "Async
polymorphism" (ie. the ability to specific @async/noAsync
type-parametrically), but either way I'd love to see how it worked
out.
Cheers,
Miles
Mon, 2011-12-05, 14:57
#3
Re: Proposal for helping out with concurrency
(premature click)
That way something like @pure/ @unpure could also be introduced.
On Mon, Dec 5, 2011 at 2:42 PM, Patrik Andersson <pandersson@gmail.com> wrote:
That way something like @pure/ @unpure could also be introduced.
On Mon, Dec 5, 2011 at 2:42 PM, Patrik Andersson <pandersson@gmail.com> wrote:
Wouldn't it be even better to come up with someting generic such as a meta annotation that indicates whether annotation type A may be enclosed within type B or not. That way you could specify that @noAsync must never be called from something that is annotated with @async.
On Mon, Dec 5, 2011 at 2:34 PM, √iktor Ҡlang <viktor.klang@gmail.com> wrote:People of the Scala language,
To prevent from unknowingly closing over non-threadsafe mutable state, I propose the following:
Adding @noAsync and @async, so you can tag that a closure/function parameter is @async, and then the compiler will barf if it closes over something annotated with @noAsync.
You may also declare a reference type as @async, which makes it eligible for closing over even if the parent is marked as @noAsync
@noAsync class Actor {
val self = ActorRef @async = ... //Means that it's OK to close over self
val foo = 2 //Since owning class is @noAsync, this is not legal to close over when closure is @async
}
class MyActor extends Actor {
def receive = {
case "foo" => Future { foo } //Illegal, since Future[T](thunk: T @async) requires what is closed over to be @async, or not have any parent that is @noAsync
}
}
This would also weed out accidental issues with ParallelCollections.
Ideas? Thoughts? Rants?
Cheers,
√
--
Viktor Klang
Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts
Twitter: @viktorklang
Mon, 2011-12-05, 21:17
#4
Re: Proposal for helping out with concurrency
2011/12/5 √iktor Ҡlang <viktor.klang@gmail.com>
I've often wished for a "mutable state escapes into closure" warning that I could leave on by default and suppress on a per-method or per-file level.
Either way, doesn't this sound like a job for the* effects system?
-0xe1a
* for some values of "the", https://github.com/lrytz/effects
People of the Scala language,
To prevent from unknowingly closing over non-threadsafe mutable state ...
I've often wished for a "mutable state escapes into closure" warning that I could leave on by default and suppress on a per-method or per-file level.
Either way, doesn't this sound like a job for the* effects system?
-0xe1a
* for some values of "the", https://github.com/lrytz/effects
On Mon, Dec 5, 2011 at 2:34 PM, √iktor Ҡlang <viktor.klang@gmail.com> wrote: