-
Notifications
You must be signed in to change notification settings - Fork 47
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
Changes from 4 commits
5912627
dbcd0c3
dcce28b
a2b97d6
cea4c9d
154dc75
ad8e49a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,9 @@ package plotly | |
|
|
||
| import plotly.element.LocalDateTime | ||
|
|
||
| import scala.collection.{Seq => BaseScalaSeq} | ||
| import scala.collection.immutable.Seq | ||
|
|
||
| sealed abstract class Sequence extends Product with Serializable | ||
|
|
||
| object Sequence { | ||
|
|
@@ -27,4 +30,28 @@ object Sequence { | |
| Strings(s) | ||
| implicit def fromDateTimes(seq: Seq[LocalDateTime]): Sequence = | ||
| DateTimes(seq) | ||
|
|
||
| implicit def fromMutableDoubleSeq(s: BaseScalaSeq[Double]): Sequence = | ||
| Doubles(makeImmutableSeq(s)) | ||
| implicit def fromMutableFloatSeq(s: BaseScalaSeq[Float]): Sequence = | ||
| Doubles(makeImmutableSeq(s.map(_.toDouble))) | ||
| implicit def fromMutableIntSeq(s: BaseScalaSeq[Int]): Sequence = | ||
| Doubles(makeImmutableSeq(s.map(_.toDouble))) | ||
| implicit def fromMutableLongSeq(s: BaseScalaSeq[Long]): Sequence = | ||
| Doubles(makeImmutableSeq(s.map(_.toDouble))) | ||
| implicit def fromMutableNestedDoubleSeq(s: BaseScalaSeq[BaseScalaSeq[Double]]): Sequence = | ||
| NestedDoubles(makeImmutableSeq(s.map(makeImmutableSeq))) | ||
| implicit def fromMutableNestedIntSeq(s: BaseScalaSeq[BaseScalaSeq[Int]]): Sequence = | ||
| NestedInts(makeImmutableSeq(s.map(makeImmutableSeq))) | ||
| implicit def fromMutableStringSeq(s: BaseScalaSeq[String]): Sequence = | ||
| Strings(makeImmutableSeq(s)) | ||
| implicit def fromMutableDateTimes(seq: BaseScalaSeq[LocalDateTime]): Sequence = | ||
| DateTimes(makeImmutableSeq(seq)) | ||
|
|
||
| private def makeImmutableSeq[A](mutableSeq: BaseScalaSeq[A]): Seq[A] = | ||
| mutableSeq match { | ||
| case asImmutableSeq: Seq[A] => asImmutableSeq | ||
| case _ => mutableSeq.toVector | ||
| } | ||
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package plotly | ||
|
|
||
| import scala.collection.immutable.Seq | ||
|
||
| import scala.language.implicitConversions | ||
|
|
||
| import java.lang.{ Boolean => JBoolean, Double => JDouble } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package plotly | ||
|
|
||
| import scala.collection.immutable.Seq | ||
| import org.joda.time._ | ||
| import plotly.Sequence.DateTimes | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package plotly | ||
|
|
||
| import org.scalatest.FlatSpec | ||
|
|
||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
| class SequenceTests extends FlatSpec { | ||
|
|
||
| "The implicit sequence conversion" should "convert a List to a Sequence" in { | ||
| assert((List(1, 2, 3): Sequence) === Sequence.Doubles(List(1d, 2d, 3d))) | ||
| } | ||
|
|
||
| it should "convert a mutable ArrayBuffer to a Sequence" in { | ||
| assert((ArrayBuffer(1, 2, 3): Sequence) === Sequence.Doubles(List(1d, 2d, 3d))) | ||
| } | ||
|
|
||
| it should "convert a nested mutable ArrayBuffer to a Sequence" in { | ||
| val mutableNestedDoubles: ArrayBuffer[ArrayBuffer[Double]] = ArrayBuffer( | ||
| ArrayBuffer(1d, 2d), | ||
| ArrayBuffer(3d, 4d), | ||
| ) | ||
|
|
||
| val nestedDoublesList: List[List[Double]] = List( | ||
| List(1d, 2d), | ||
| List(3d, 4d), | ||
| ) | ||
|
|
||
| assert((mutableNestedDoubles: Sequence) === Sequence.NestedDoubles(nestedDoublesList)) | ||
| } | ||
|
|
||
| } |
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 acceptSeq(…)in both cases, which is not the case in 2.12 if we requirescala.collection.immutable.Seqexplicitly.