A little depentent match type function question. I managed to solve the problem actually via a delegate (a custom If
construct), but why that is necessary, and if it could be overcome, would be of much interest for me going forward!
Full context is here with a little more explanation of what I’m trying to do: Scastie - An interactive playground for Scala.
Problem: this function does not work, to my dismay:
type IsAnOutputT3[N, Edges] <: Boolean = Edges match
case h *: t => IsOutputT[N,h] match
case true => true
case false => IsAnOutputT3[N,t]
case EmptyTuple => false
def isAnOutput3[N, Edges <: Tuple](node: N, edges: Edges): IsAnOutputT3[N,Edges] =
edges match
case tup: *:[h, t] => isOutput[N,h](node, tup.head) match
case _: true => true
case _: false => isAnOutput3(node, tup.tail)
case _: EmptyTuple => false
while this does with the help of the If
/ifthenelse
construct:
type IsAnOutputT2[N, Edges] <: Boolean = Edges match
case h *: t => If[
h,
[X] =>> IsOutputT[N, X],
true,
IsAnOutputT2[N, t]
]
case EmptyTuple => false
inline def isAnOutput2[N, Edges <: Tuple](node: N, edges: Edges): IsAnOutputT2[N,Edges] =
inline edges match
case tup: *:[h, t] => {
ifThenElse2(
(tup.head: h),
[Edge] => (c: Edge) => isOutput[N,Edge](node,c),
true,
isAnOutput2(node, tup.tail)
)
}
case _: EmptyTuple => false
type If[C, P[_] <: Boolean, A, B] <: A | B = P[C] match
case true => A
case false => B
def ifThenElse2[C, P[_] <: Boolean, A, B](cond: C, predicate: [CC <: C] => (CC) => P[CC], thenVal: => A, elseVal: => B): If[C, P, A, B] =
predicate(cond) match
case _: true => thenVal
case _: false => elseVal
but the latter is way clunky I think. I would hate to proceed with that style.