Skip to content

Commit 6e73084

Browse files
committed
generic form of make
1 parent 852758b commit 6e73084

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

src/main/scala/gopher/GopherAPI.scala

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class GopherAPI(as: ActorSystem, es: ExecutionContext)
3333
def select: SelectFactory =
3434
new SelectFactory(this)
3535

36+
def make[T](args: Any*): T = macro GopherAPI.makeImpl[T]
37+
3638
/**
3739
* obtain channel
3840
*
@@ -41,18 +43,9 @@ class GopherAPI(as: ActorSystem, es: ExecutionContext)
4143
* channel.awrite(1 to 100)
4244
*}}}
4345
*/
46+
@inline
4447
def makeChannel[A](capacity: Int = 0): Channel[A] =
45-
{
46-
require(capacity >= 0)
47-
val nextId = newChannelId
48-
val futureChannelRef = (channelSupervisorRef.ask(
49-
NewChannel(nextId, capacity)
50-
)(10 seconds)
51-
.asInstanceOf[Future[ActorRef]]
52-
)
53-
54-
new ActorBackedChannel[A](futureChannelRef, this)
55-
}
48+
Channel[A](capacity)(this)
5649

5750
def makeEffectedInput[A](in: Input[A], threadingPolicy: ThreadingPolicy = ThreadingPolicy.Single): EffectedInput[A] =
5851
EffectedInput(in,threadingPolicy)
@@ -144,6 +137,17 @@ class GopherAPI(as: ActorSystem, es: ExecutionContext)
144137
object GopherAPI
145138
{
146139

140+
def makeImpl[T : c.WeakTypeTag](c:Context)(args: c.Expr[Any]*): c.Expr[T] = {
141+
import c.universe._
142+
val wt = weakTypeOf[T]
143+
if (wt.companion =:= NoType) {
144+
c.abort(c.prefix.tree.pos,s"type ${wt.typeSymbol} have no companion")
145+
}
146+
val sym = wt.typeSymbol.companion
147+
val r = q"${sym}.apply[..${wt.typeArgs}](..${args})(${c.prefix})"
148+
c.Expr[T](r)
149+
}
150+
147151
def makeTransputerImpl[T <: Transputer : c.WeakTypeTag](c:Context):c.Expr[T] = {
148152
import c.universe._
149153
c.Expr[T](q"${c.prefix}.makeTransputer[${weakTypeOf[T]}](gopher.Transputer.RecoveryPolicy.AlwaysRestart)")

src/main/scala/gopher/channels/Channel.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,21 @@ trait Channel[A] extends InputOutput[A]
1919

2020
}
2121

22+
object Channel
23+
{
24+
25+
def apply[A](capacity: Int = 0)(implicit api:GopherAPI):Channel[A] =
26+
{
27+
require(capacity >= 0)
28+
import api._
29+
val nextId = newChannelId
30+
val futureChannelRef = (channelSupervisorRef.ask(
31+
NewChannel(nextId, capacity)
32+
)(10 seconds)
33+
.asInstanceOf[Future[ActorRef]]
34+
)
35+
36+
new ActorBackedChannel[A](futureChannelRef, api)
37+
}
38+
39+
}

src/test/scala/gopher/channels/MacroSelectSuite.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ class MacroSelectSuite extends FunSuite
260260
assert(n2i>3)
261261
}
262262

263+
test("generic channel make") {
264+
val ch1 = gopherApi.make[Channel[Int]]()
265+
val ch2 = gopherApi.make[Channel[Int]](1)
266+
// yet not supported by compiler.
267+
//val ch3 = gopherApi.make[Channel[Int]](capacity=3)
268+
val f1 = ch1.awrite(1)
269+
val f2 = ch2.awrite(2)
270+
val x = Await.result(ch1.aread, 10 seconds)
271+
assert(x==1)
272+
}
273+
263274
lazy val gopherApi = CommonTestObjects.gopherApi
264275

265276
}

0 commit comments

Comments
 (0)