Create an app:
play new foo
cd foo
Add dependencies to project/Build.scala:
| import Control.Proxy | |
| import Control.Proxy.TCP | |
| main :: IO () | |
| main = serve (Host "0.0.0.0") "8000" $ \(socket,_) -> | |
| runProxy $ socketReadS 4096 socket >-> socketWriteD socket |
Have you ever had to write code that made a complex series of succesive modifications to a single piece of mutable state? (Almost certainly yes.)
Did you ever wish you could make the compiler tell you if a particular operation on the state was illegal at a given point in the modifications? (If you're a fan of static typing, probably yes.)
If that's the case, the indexed state monad can help!
Motivation
| import language.experimental.macros | |
| import language.implicitConversions | |
| import scala.reflect.macros.Context | |
| import scala.reflect.runtime.universe.Tree | |
| class ReflectiveClosure[A, B](val tree: Tree, fn: Function1[A, B]) extends Function1[A, B] { | |
| def apply(x: A) = fn(x) | |
| } | |
| object ReflectiveClosure { |
| import scala.reflect.macros.Context | |
| import language.experimental.macros | |
| case class EnclosingApplication(tree: scala.reflect.runtime.universe.Tree) | |
| object Macros { | |
| def impl(c: Context): c.Expr[EnclosingApplication] = { | |
| import c.universe._ | |
| import treeBuild._ |
| {-# LANGUAGE Rank2Types | |
| , RebindableSyntax | |
| , ImplicitParams | |
| , NoMonomorphismRestriction #-} | |
| import Data.Maybe | |
| import Data.Function | |
| import Data.String | |
| import Prelude (undefined, error, String, (++)) |
| Answers http://stackoverflow.com/questions/10373318/mixing-in-a-trait-dynamically. | |
| Compile as follows: | |
| scalac Common_1.scala Macros_2.scala | |
| scalac Common_1.scala Test_3.scala -cp <path to the result of the previous compilation> | |
| Tested in 2.10.0-M3, will most likely not compile by the time 2.10.0 final is released, because we're actively rehashing the API. | |
| However the principles will remain the same in the final release, so the concept itself is okay. | |
| upd. Code updated for 2.10.0-M7. | |
| upd. Code updated for 2.10.0-RC1. |
| class A | |
| class A2 extends A | |
| class B | |
| trait M[X] | |
| // | |
| // Upper Type Bound | |
| // | |
| def upperTypeBound[AA <: A](x: AA): A = x |