Closed
Description
Compiler version
3.0.1
Minimized code
Hint: when I comment out the reversed
method, everything compiles just fine
object Implementing_Tuples:
sealed trait Tup
case class ConsTup[T, H <: Tup](head: T, tail: H) extends Tup
case object EmptyTup extends Tup
val *: = ConsTup // for unapply
type *:[H, T <: Tup] = ConsTup[H, T] // for type matching
type EmptyTup = EmptyTup.type // for type matching
extension [H](head: H)
def *:[T <: Tup](tail: T) = ConsTup(head, tail)
type Fold[T <: Tup, Seed, F[_,_]] = T match
case EmptyTup => Seed
case h *: t => Fold[t, F[Seed, h], F]
extension [T <: Tup](v: T)
def fold[Seed, F[_,_]](seed: Seed)(
fn: [C, Acc] => (C, Acc) => F[C, Acc]
): Fold[T, Seed, F] =
(v match
case EmptyTup => seed
case h *: t => t.fold(fn(h, seed))(fn)
).asInstanceOf[Fold[T, Seed, F]]
extension [T <: Tup](v: T) def reversed =
v.fold[EmptyTup, [C, Acc] =>> Acc match {
case h *: t => C *: h *: t
}](EmptyTup)(
[C, Acc] => (c: C, acc: Acc) => acc match
case _@(_ *: _) => c *: acc
)
@main def testProperFold =
val t = (1 *: '2' *: "foo" *: EmptyTup)
val reversed: (String *: Char *: Int *: EmptyTup) = t.reversed
println(reversed)
end Implementing_Tuples
Output (click arrow to expand)
Heavily cropped because the stack trace is quite large
java.lang.AssertionError: assertion failed
at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:11)
at dotty.tools.dotc.core.SymDenotations$SymDenotation.extensionParam(SymDenotations.scala:341)
at dotty.tools.dotc.typer.Typer.isLocalExtensionMethodRef$3$$anonfun$2(Typer.scala:524)
at dotty.tools.dotc.core.Denotations$SingleDenotation.hasAltWith(Denotations.scala:637)
at dotty.tools.dotc.core.Denotations$MultiDenotation.hasAltWith(Denotations.scala:1251)
at dotty.tools.dotc.typer.Typer.isLocalExtensionMethodRef$1(Typer.scala:524)
at dotty.tools.dotc.typer.Typer.typedIdent(Typer.scala:530)
at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2664)
at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2757)
at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2823)
at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2827)
at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2943)
at dotty.tools.dotc.typer.Typer.typeSelectOnTerm$1(Typer.scala:609)
at dotty.tools.dotc.typer.Typer.typedSelect(Typer.scala:659)
at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2665)
at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2757)
at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2823)
at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2827)
at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2943)
at dotty.tools.dotc.typer.Applications.tryWithProto$1(Applications.scala:1256)
........