Awhile back I asked why kotlin did not add python-...
# language-evolution
h
Awhile back I asked why kotlin did not add python-esque indexing of arrays/lists, e.g.
Copy code
val nums = listOf(1, 2, 3, 4, 5, 6)
val firstThree = nums[..2]
val lastThree = nums[-3..]
val trimEndsByTwo = nums[2..-3]
And the issue that I remember (note: not the only issue, perhaps) is that
indexOf
returns
-1
on an unfound element. so
Copy code
val elementsToSeven = nums[..indexOf(7)]
would return the entirety of
nums
, an unexpected value C# 8.0 skirts around this issue by using
^
instead of
-
as the end-orientation operator, and throws out of range for negative indices.
Copy code
val lastThree = nums[^3..]
Would kotlin benefit from the same approach? I believe so. Especially given
^
is not currently used at all
C# feels like a language halfway between Java and Kotlin, and this feature (as well as partials) marks the first major feature of the language I feel Kotlin is missing
Of course, pointers, structs, and interaction with the gc are nice, but I've never felt the need to use them
j
i have gotten a little bit of comfort from indexing varargs and iterables but the syntax blocks python ranges without specifying both sides
h
Yep, I found the same problem. And unfortunately outside of indexing I can't think of any way to allow for unbounded ranges without confusing the compiler
j
@elizarov made a comment that an unbounded ".." is not off the table completely but he wants to see a quick hack of the implementation, i think he said it might take one day.
h
I just don't see how the compiler decide whether or not
..
is unbounded or just bonded to something on the unbounded side
j
for
val firstThree = nums[..2]
first is required
operator T.get(iterable<Int>)
as i have for reordering indexes. next i guess is a Unary op for ".."(Int)->Iterator for y[2..] we need infix fun Int.``..
(Nothing)
as a guess anyways. but then the grammar needs to elide parsing a token for this special infix operator fun. i cannot think of similar existing operators within the existing grammar
what T does with negative indexes ought to be T's problem to figure out.
h
Oh, well I've also wanted unbounded ranges outside of indexing
Copy code
val fourToInfinity = 4..
val negativeInfinityToSix = ..6
i think the compiler wouldn't have a problem with that much, but what about:
Copy code
val fourToInfinity = 4..
myData.id += 1
That would require a semicolon for only the second time in the whole language. I think I'd be okay with that concession, though
j
or parens
h
(1..)
you mean? yeah, i'm fine with that
k
If we had custom subscripts like Swift, where labels can distinguish these, we could just have
list[7]
,
list[wrapping = -2]
,
list[clamping = 2000]
,
list[orNull = 50]
, etc.
j
that looks really cool
k
It is! Very handy too
h
Swift looks like such a fantastic language, but I really don't wanna support Apple's awful anti-developer practice of requiring you write software for their devices using an Apple product.
k
@Hullaballoonatic Ehhhh I can kinda understand that. But it would be nice if this open-source langauge, which can be compiled on Linux, could at least target those devices when compiled on Linux, even if that means the result can't go on the App Store. 😞