Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is there/can there be a tailcall optimisation for match types? #23130

Unanswered
bishabosha asked this question in Compiler internals Q&A
Discussion options

step 1. copy to clipboard a 200 elem tuple.
scala -e 'println((1 to 200).mkString("(", ",", ")"))' | pbcopy

start repl with a small stack size

$ scala -J -Xss512k
scala>val tup = <paste>
scala>summon[Tuple.Last[tup.type] =:= Int]
-- Error: ----------------------------------------------------------------------
1 |summon[Tuple.Last[tup.type] =:= Int]
 | ^
 |Recursion limit exceeded.
 |Maybe there is an illegal cyclic reference?
 |If that's not the case, you could also try to increase the stacksize using the -Xss JVM option.
 |For the unprocessed stack trace, compile with -Xno-enrich-error-messages.
 |A recurring operation is (inner to outer):
 |
 | reduce type (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
 | Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
 | Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
 | Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
 | Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
 | Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
 | Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
 | Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int,
 | Int, Int, Int, Int, Int, Int) match ...

surely Tuple.Last shouldn't blow the stack because internally its representation as a match type can be turned into a loop?

object Tuple {
 ...
 /** Type of the last element of a tuple */
 type Last[X <: Tuple] = X match {
 case x *: EmptyTuple => x
 case _ *: xs => Last[xs]
 }

as it is unfortunately most of the tuple match types are not written in a tail recursive way if this optimisation was ever brought to production, are they frozen for compatibility or only the bound matters?

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /