is
!is
- is
!is
:
if (obj is String) {
print(obj.length)
}
if (obj !is String) { // !(obj is String)
print("Not a String")
}
else {
print(obj.length)
}
, Kotlin , is
-
, .
fun demo(x: Any) {
if (x is String) {
print(x.length) // x String
}
}
, , (!is
)
:
if (x !is String) return
print(x.length) // x String
, &&
||
:
// x String `||`
if (x !is String || x.length == 0) return
// x is String `&&`
if (x is String && x.length > 0) {
print(x.length) // x String
}
when (x) {
is Int -> print(x + 1)
is String -> print(x.length + 1)
is IntArray -> print(x.sum())
}
, , . , :
private
internal
, , .
, , getter';, . , . Kotlin as:
val x: String = y as String
, null String
, String
nullable,
.. y
- null, . Java, nullable :
val x: String? = y as String?
, as?, null :
val x: String? = y as? String
, , as? non-null String
, nullable.
rangeTo
..
, in !in.
(comparable) , . .
if (i in 1..10) { // equivalent of 1 <= i && i <= 10
println(i)
}
(IntRange
, LongRange
, CharRange
) : .
for Java.
for (i in 1..4) print(i) // prints "1234"
for (i in 4..1) print(i) // prints nothing
, ? . downTo()
, .
for (i in 4 downTo 1) print(i) // prints "4321"
, ? . step()
:
for (i in 1..4 step 2) print(i) // prints "13"
for (i in 4 downTo 1 step 2) print(i) // prints "42"
, , until
:
for (i in 1 until 10) { // i in [1, 10), 10 is excluded
println(i)
}
ClosedRange<T>
.
, ClosedRange<T>
, .
: start
endInclusive
. contain
. in/!in.
(IntProgression
, LongProgression
, CharProgression
) .
first
, last
increment
.
first
, , increment
.
, last
.
Iterable<N>
, N
- Int
, Long
Char
. , for map
, filter
..
Progression
for Java/JavaScript
for (int i = first; i != last; i += increment) {
// ...
}
, ..
, ClosedRange<T>
*Progression*
.
, IntRange
IntProgression
ClosedRange<Int>
. , IntProgression
,
IntRange
. downTo()
step()
*Progression*
(.: ).
fromClosedRange
(companion object):
IntProgression.fromClosedRange(start, end, increment)
last
. ,
end
. , - .
rangeTo()
rangeTo()
*Range*
:
class Int {
//...
operator fun rangeTo(other: Long): LongRange = LongRange(this, other)
//...
operator fun rangeTo(other: Int): IntRange = IntRange(this, other)
//...
}
(Double
, Float
) rangeTo
. Comparable
:
public operator fun <T: Comparable<T>> T.rangeTo(that: T): ClosedRange<T>
, , .
downTo()
- downTo()
, :
fun Long.downTo(other: Int): LongProgression {
return LongProgression.fromClosedRange(this, other, -1.0)
}
fun Byte.downTo(other: Int): IntProgression {
return IntProgression.fromClosedRange(this, other, -1)
}
reversed()
reversed()
*Progression*
, .
fun IntProgression.reversed(): IntProgression {
return IntProgression.fromClosedRange(last, first, -increment)
}
step()
- step()
*Progression*
,
step
( ).
, .
fun IntProgression.step(step: Int): IntProgression {
if (step <= 0) throw IllegalArgumentException("Step must be positive, was: $step") //
return IntProgression.fromClosedRange(first, last, if (increment > 0) step else -step)
}
fun CharProgression.step(step: Int): CharProgression {
if (step <= 0) throw IllegalArgumentException("Step must be positive, was: $step")
return CharProgression.fromClosedRange(first, last, step)
}
, last
last
,
(last - first) % increment == 0
. :
(1..12 step 2).last == 11 // [1, 3, 5, 7, 9, 11]
(1..12 step 3).last == 10 // [1, 4, 7, 10]
(1..12 step 4).last == 9 // [1, 5, 9]
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |