Skip to content

Commit 7923c0f

Browse files
Resolve ambigious implicits under new Scala 3.6 givens prioritization betwen mapWriter and collectionWriter (#415)
1 parent 4b0ba58 commit 7923c0f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

api/src/main/scala-3/BSONWriterInstances.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ private[bson] trait BSONWriterInstances extends BSONWriterInstancesLowPrio:
112112
bsonUndefinedWriter,
113113
bsonRegexWriter,
114114
bsonJavaScriptWriter,
115-
bsonJavaScriptWSWriter,
116-
collectionWriter
115+
bsonJavaScriptWSWriter
117116
}
118117

119118
given tuple2Writer[A: BSONWriter, B: BSONWriter]: BSONWriter[(A, B)] =
@@ -141,5 +140,16 @@ end BSONWriterInstances
141140
private[bson] sealed trait BSONWriterInstancesLowPrio {
142141
_self: BSONWriterInstances =>
143142

143+
/*
144+
* Since Scala 3.6 the order of givens prioritization have changed - compiler can choose the most general implicit instead of the most specific
145+
* For example when searching for Writer[Map[String, T]]
146+
* - Scala 3.5 chooses mapWriter: BSONDocumentWriter[Map[String, V]] instead of collectionWriter[T, Repr <: Iterable[T]]: BSONWriter[Repr] as it's more specyfic
147+
* - Scala 3.6 sees both of the candidates as equally good leading to ambigious implicit search result
148+
* To prevent this issue for Scala 3.6+ users more generalized writers should be put in the low-priority trait to workaround these changes.
149+
* It would have no effect for Scala 3.5- users since the more specific implicit would choosen anyway
150+
* See Scala 3 PR that introduced this change for more info: https://github.com/scala/scala3/pull/19300
151+
*/
152+
export pkg.collectionWriter
153+
144154
given bsonValueIdentityWriter: BSONWriter[BSONValue] = BSONValueIdentity
145155
}

0 commit comments

Comments
 (0)