Skip to content

Commit dff5b3d

Browse files
committed
Simplify some generation logic of GenerateFunctionConverters
1 parent 7ed7123 commit dff5b3d

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

library/src/scala/Function0.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
// GENERATED CODE: DO NOT EDIT.
14-
// genprod generated these sources at: 2025-08-11T16:09:05.385173621Z
14+
// genprod generated these sources at: 2025-08-11T16:29:31.823591209Z
1515

1616
package scala
1717

project/GenerateFunctionConverters.scala

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ object GenerateFunctionConverters {
119119
def text = Predef.augmentString(showCode(t).replace("$", "")).lines.toVector
120120
}
121121

122+
/** Blocks of code with priority (lower priority has higher precedence in implicit search). */
122123
case class Prioritized(lines: Vector[String], priority: Int) {
123124
def withPriority(i: Int) = copy(priority = i)
124125
}
125126

127+
/** Contains code for conversion between functions and SAMs (single abstract methods). */
126128
case class SamConversionCode(
127129
base: String,
128130
wrappedAsScala: Vector[String],
@@ -140,20 +142,21 @@ object GenerateFunctionConverters {
140142
}
141143
object SamConversionCode {
142144
def apply(scc: SamConversionCode*): (Vector[String], Vector[String], Vector[Vector[String]]) = {
143-
val sccDepthSet = scc.map(_.implicitToJava.priority).toSet
145+
val sccDepthSet = scc.map(_.implicitToJava.priority).to[collection.SortedSet]
144146
val codes =
145147
{
146-
if (sccDepthSet != (0 to sccDepthSet.max).toSet) {
147-
val sccDepthMap = sccDepthSet.toList.sorted.zipWithIndex.toMap
148+
val size = sccDepthSet.size
149+
// normalize the elements to a fix [0..size-1] range
150+
if (sccDepthSet.firstKey != 0 || sccDepthSet.lastKey != size - 1) {
151+
val sccDepthMap = sccDepthSet.iterator.zipWithIndex.toMap
148152
scc.map(x => x.withPriority(sccDepthMap(x.implicitToJava.priority)))
149153
}
150154
else scc
151155
}.toVector.sortBy(_.base)
152-
def priorityName(n: Int, pure: Boolean = false): String = {
153-
val pre =
154-
if (pure) s"Priority${n}FunctionExtensions"
155-
else s"trait ${priorityName(n, pure = true)}"
156-
if (!pure && n < (sccDepthSet.size-1)) s"$pre extends ${priorityName(n+1, pure = true)}" else pre
156+
def priorityName(n: Int): String = s"Priority${n}FunctionExtensions"
157+
def priorityHeader(n: Int): String = {
158+
val pre = s"trait ${priorityName(n)}"
159+
if (n < sccDepthSet.size - 1) s"$pre extends ${priorityName(n+1)}" else pre
157160
}
158161
val impls =
159162
"object FunctionWrappers {" +: {
@@ -163,7 +166,7 @@ object GenerateFunctionConverters {
163166
val traits = codes.groupBy(_.implicitToJava.priority).toVector.sortBy(- _._1).map{ case (k,vs) =>
164167
s"import language.implicitConversions" +:
165168
"" +:
166-
s"${priorityName(k)} {" +:
169+
s"${priorityHeader(k)} {" +:
167170
s" import FunctionWrappers._" +:
168171
s" " +:
169172
{

0 commit comments

Comments
 (0)