Introduce contentEquals/HashCode/ToString for Arrays. Different methods for deep equals/hashCode/toString rather than overloads with a parameter, because most of the time it would be constant, and make these methods inline-only.

Make `contentEquals` infix function.
Add docs.
#KT-13582
This commit is contained in:
Ilya Gorbunov
2016-09-21 07:04:19 +03:00
parent ab2facf030
commit a868eecb1a
2 changed files with 384 additions and 0 deletions
@@ -44,6 +44,102 @@ fun arrays(): List<GenericFunction> {
}
}
templates add f("contentEquals(other: SELF)") {
only(ArraysOfObjects, ArraysOfPrimitives)
jvmOnly(true)
inline(Inline.Only)
infix(true)
doc {
"""
Returns `true` if the two specified arrays are *structurally* equal to one another,
i.e. contain the same number of the same elements in the same order.
"""
}
returns("Boolean")
body {
"return Arrays.equals(this, other)"
}
}
templates add f("contentDeepEquals(other: SELF)") {
only(ArraysOfObjects)
jvmOnly(true)
inline(Inline.Only)
infix(true)
doc {
"""
Returns `true` if the two specified arrays are *deeply* equal to one another,
i.e. contain the same number of the same elements in the same order.
If two corresponding elements are nested arrays, they are also compared deeply.
If any of arrays contains itself on any nesting level the behavior is undefined.
"""
}
returns("Boolean")
body {
"return Arrays.deepEquals(this, other)"
}
}
templates add f("contentToString()") {
only(ArraysOfObjects, ArraysOfPrimitives)
jvmOnly(true)
inline(Inline.Only)
doc { "Returns a string representation of the contents of the specified array as if it is [List]." }
returns("String")
body {
"return Arrays.toString(this)"
}
}
templates add f("contentDeepToString()") {
only(ArraysOfObjects)
jvmOnly(true)
inline(Inline.Only)
doc {
"""
Returns a string representation of the contents of this array as if it is [List].
Nested arrays are treated as lists too.
"""
}
returns("String")
body {
"return Arrays.deepToString(this)"
}
}
templates add f("contentHashCode()") {
only(ArraysOfObjects, ArraysOfPrimitives)
jvmOnly(true)
inline(Inline.Only)
doc {
"Returns a hash code based on the contents of this array as if it is [List]."
}
returns("Int")
body {
"return Arrays.hashCode(this)"
}
}
templates add f("contentDeepHashCode()") {
only(ArraysOfObjects)
jvmOnly(true)
inline(Inline.Only)
doc {
"""
Returns a hash code based on the contents of this array as if it is [List].
Nested arrays are treated as lists too.
If any of arrays contains itself on any nesting level the behavior is undefined.
"""
}
returns("Int")
body {
"return Arrays.deepHashCode(this)"
}
}
templates addAll PrimitiveType.defaultPrimitives.map { primitive ->
val arrayType = primitive.name + "Array"
f("to$arrayType()") {