Skip to content
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

Restore Array.deep from ArrayLike #6912

Closed
wants to merge 1 commit into from

Conversation

MasseGuillaume
Copy link
Member

@scala-jenkins scala-jenkins added this to the 2.13.0-M5 milestone Jul 10, 2018
@julienrf julienrf changed the title Restore Array.deep from ArrayLike (fix #10985) Restore Array.deep from ArrayLike Jul 10, 2018

/** Creates a possible nested `IndexedSeq` which consists of all the elements
* of this array. If the elements are arrays themselves, the `deep` transformation
* is applied recursively to them. The `stringPrefix` of the `IndexedSeq` is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be className instead of stringPrefix

case x: Array[Boolean] => deep((new mutable.ArraySeq.ofBoolean(x) ).asInstanceOf[mutable.ArraySeq[Boolean]])
case x: Array[Unit] => deep((new mutable.ArraySeq.ofUnit(x) ).asInstanceOf[mutable.ArraySeq[Unit]] )
case x => x
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation in 2.12.x seems simpler: https://github.com/scala/scala/blob/2.12.x/src/library/scala/collection/mutable/ArrayLike.scala#L41-L44

Is there a reason to have all these cases?

@julienrf
Copy link
Contributor

Can you also fix the commit message?

@MasseGuillaume MasseGuillaume force-pushed the bug/10985 branch 2 times, most recently from c92030f to f5490b4 Compare July 10, 2018 13:28
@MasseGuillaume
Copy link
Member Author

@julienrf done, ci is ✔️

Copy link
Contributor

@julienrf julienrf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that we go one step further and remove this prettyArray thing from partest Utils. This will have the nice side effect of decreasing the number of blacklisted partests in Scala.js.

implicit class ArrayDeep(val a: Array[_]) extends AnyVal {
def deep: collection.IndexedSeq[Any] = prettyArray(a)
def prettyArray: collection.IndexedSeq[Any] = prettyArray0(a)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I’m thinking about something: the less code we have in this file, the more compatible with Scala.js partests are. Currently, all the tests that use the deep method that you renamed to prettyArray are blacklisted in Scala.js because this Util.scala file is not used by Scala.js, therefore the partests that use features defined in that file don’t compile.

Maybe it’s an opportunity to remove this prettyArray thing at all. Do we really need a method that wraps arbitrarily nested levels of Arrays to IndexedSeqs? I guess we usually have one or two levels of nested arrays, so the fix consists in manually calling .toSeq or .map(_.toSeq).toSeq.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we keep Array.deep without deprecation. This method is useful to print an array or check for equality.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure. To print an array I find it simple to call .toSeq. To check for equality we can use Java’s Arrays.deepEquals. What do other people think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array(Array(1)).toSeq.toString // WrappedArray([I@6fc1a8f6)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we keep it, deep is certainly a not a good name. Are there any other use cases than toString? If not, there should instead be a method that returns the string representation directly.

Personally, I think the method is not widely useful enough for the standard libarary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for pretty-printing and checking for equality in tests. If you use Arrays.deepEquals, you only have one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The very fact that deep() returns an IndexedSeq[Any] is a huge smell. It's basically not type parametric. If I manipulate an Array[T] and I call deep() on it, I am at the mercy of some of the Ts being Arrays. It's the same huge problem that JavaScript Promises face.

Kill it.

@@ -108,7 +108,7 @@ object Test4 {
import java.lang.reflect.AnnotatedElement
def printSourceAnnotation(a: Annotation): Unit = {
val ann = a.asInstanceOf[SourceAnnotation]
println("@test.SourceAnnotation(mails=" + ann.mails.deep.mkString("{", ",", "}") +
println("@test.SourceAnnotation(mails=" + ann.mails.prettyArray.mkString("{", ",", "}") +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For instance, here we can just remove deep: ann.mails.mkString("{", ",", "}")

@@ -71,7 +71,7 @@ object Test2 {
println()

def loadArray[T](x: Array[Byte])(implicit t: reflect.ClassTag[Array[T]]) =
load[Array[T]](x)(t).deep.toString
load[Array[T]](x)(t).prettyArray.toString
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we can replace deep with toSeq.

@@ -71,7 +71,7 @@ object Test2 {
println()

def loadArray[T](x: Array[Byte])(implicit m: reflect.Manifest[Array[T]]) =
load[Array[T]](x)(m).deep.toString
load[Array[T]](x)(m).prettyArray.toString
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace deep with toSeq

@julienrf
Copy link
Contributor

Why did you close the PR?

@MasseGuillaume
Copy link
Member Author

MasseGuillaume commented Jul 13, 2018

@julienrf It looks like no one is on board with the idea of keeping this method. We should add this in scala-collection-compat instead.

@julienrf
Copy link
Contributor

I don’t think we should add it to scala-collection-compat (see my comment there).

But we can simply not use it and manually migrate the projects that used to rely on it (like our partests)

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