Skip to content

Commit e8e2e7a

Browse files
committed
order caseDefs in select, to make doneCases be first during building reading callback expressions.
This is needed for case, when done event was fired and we attach build expression to it.
1 parent 632445b commit e8e2e7a

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

shared/src/main/scala/gopher/Select.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ object Select:
106106
type Monad[X] = F[X]
107107
def appended[L <: SelectListeners[F,S,R] : Type](base: Expr[L])(using Quotes): Expr[L]
108108

109-
case class ReadExpression[F[_]:Type, A:Type, S:Type, R:Type](ch: Expr[ReadChannel[F,A]], f: Expr[A => S]) extends SelectorCaseExpr[F,S,R]:
109+
case class ReadExpression[F[_]:Type, A:Type, S:Type, R:Type](ch: Expr[ReadChannel[F,A]], f: Expr[A => S], isDone: Boolean) extends SelectorCaseExpr[F,S,R]:
110110
def appended[L <: SelectListeners[F,S,R]: Type](base: Expr[L])(using Quotes): Expr[L] =
111111
'{ $base.onRead($ch)($f) }
112112

@@ -183,7 +183,16 @@ object Select:
183183
//if (caseExprs.find(_.isInstanceOf[DefaultExpression[?]]).isDefined) {
184184
// report.error("default is not supported")
185185
//}
186-
builder(cases.map(parseCaseDef[F,A,B](_)))
186+
val unorderedCases = cases.map(parseCaseDef[F,A,B](_))
187+
// done should be
188+
val (isDone,notDone) = unorderedCases.partition{ x =>
189+
x match
190+
case DoneExression(_,_) => true
191+
case ReadExpression(_,_,isDone) => isDone
192+
case _ => false
193+
}
194+
val doneFirstCases = isDone ++ notDone
195+
builder(doneFirstCases)
187196

188197

189198
def parseCaseDef[F[_]:Type,S:Type,R:Type](using Quotes)(caseDef: quotes.reflect.CaseDef): SelectorCaseExpr[F,S,R] =
@@ -195,7 +204,11 @@ object Select:
195204
val readFun = makeLambda(valName,tp,bind.symbol,caseDef.rhs)
196205
if (channel.tpe <:< TypeRepr.of[ReadChannel[F,?]])
197206
tp.asType match
198-
case '[a] => ReadExpression(channel.asExprOf[ReadChannel[F,a]],readFun.asExprOf[a=>S])
207+
case '[a] =>
208+
val isDone = channel match
209+
case quotes.reflect.Select(ch1,"done") if (ch1.tpe <:< TypeRepr.of[ReadChannel[F,?]]) => true
210+
case _ => false
211+
ReadExpression(channel.asExprOf[ReadChannel[F,a]],readFun.asExprOf[a=>S],isDone)
199212
case _ =>
200213
reportError("can't determinate read type", caseDef.pattern.asExpr)
201214
else

0 commit comments

Comments
 (0)