-
Notifications
You must be signed in to change notification settings - Fork 48
Explicitly use scala.collection.Seq to allow use of mutable collections in Scala 2.13 #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explicitly use scala.collection.Seq to allow use of mutable collections in Scala 2.13 #176
Conversation
Test fails for ./sbt tests/test, passes for ./sbt ++2.12.10! tests/test
Scala 2.12 aliases Seq to scala.collection.Seq, whereas Scala 2.13 aliases to scala.collection.immutable.Seq. Explicitly using scala.collection.Seq allows the use of both mutable and immutable sequences, and retains the behaviour before the Scala 2.13 migration.
@tmccarthy Thanks for opening this! AFAIC I've always tended to rely on immutable collections when using plotly-scala, so I didn't run into that problem. I think using immutable collections may actually be more consistent. All classes in plotly-scala are already immutable, and have Do you think it would be possible to go the other way around in your uses of plotly-scala, and only pass immutable collections to it? Or is it really too cumbersome? My understanding is that calling |
Actually, the implicit conversion from mutable Seq to to |
Thanks for the response Alex! In Scala 2.12 and earlier, To be clear, I'm all in favour of targeting immutable collections. The Seeing as I'm the one that introduced this problem in #121, I thought I should fix it by restoring the previous behaviour 😅. That behaviour was to use the potentially-mutable I agree that it would be a nicer approach to make all sequences within |
…ons from mutable sequences to plotly.Sequence, with Scala-version-specific implementations.
…t once on the Sequence companion. Previous solution was over-engineered.
private def makeImmutableSeq[A](mutableSeq: BaseScalaSeq[A]): Seq[A] = | ||
mutableSeq match { | ||
case asImmutableSeq: Seq[A] => asImmutableSeq | ||
case _ => mutableSeq.toVector | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is basically equivalent to the .toSeq
method in Scala 2.13. Since we are cross-compiling with Scala 2.12, we require a dedicated implementation here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just added a comment about keeping the default Seq
by default (except in the Sequences
conversions).
@@ -4,6 +4,7 @@ import java.lang.{Boolean => JBoolean, Double => JDouble, Integer => JInt} | |||
|
|||
import almond.interpreter.api.{DisplayData, OutputHandler} | |||
|
|||
import scala.collection.immutable.Seq |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep relying on the default Seq
. That makes the API slightly different in 2.12 and 2.13, but that these APIs accept Seq(…)
in both cases, which is not the case in 2.12 if we require scala.collection.immutable.Seq
explicitly.
@@ -1,5 +1,6 @@ | |||
package plotly | |||
|
|||
import scala.collection.immutable.Seq |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, let's keep the default Seq
, even though it's slightly different in 2.12 and 2.13, here and in all scala.collection.immutable.Seq
added below.
…scala.collection.Seq` data type only in Scala 2.13 and up.
If we're happy with the split between |
That looks fine, thanks @tmccarthy! |
Since Scala 2.13, the universal
Seq
type alias has referred toscala.collection.immutable.Seq
. Prior to this, it referred toscala.collection.Seq
, which is the supertype for both mutable and immutable sequences (see migration note).Since #121, this has caused an inconsistency in how
plotly-scala
handlesSeq
between the Scala 2.13 and 2.12 versions. In Scala 2.12, mutable collections likeArrayBuffer
have an implicit conversion toplotly.Sequence
. In Scala 2.13, no conversion exists, and clients are required to convert toimmutable.Seq
. An example of code that compiles in 2.12 but not 2.13 is included in this PR as a unit test.This PR fixes this issue by explicitly importing
scala.collection.Seq
wherever it is used. In Scala 2.12 this is a no-op, sinceSeq
is simply an alias for this type. In Scala 2.13 it restores the old behaviour from before #121.Let me know if you have any questions or feedback!