https://kotlinlang.org logo
#announcements
Title
# announcements
e

edwardwongtl

10/18/2017, 8:48 AM
Check if
t
is
Iterable
inside your
concat
, and skip the
listOf(t)
is true
k

karelpeeters

10/18/2017, 9:14 AM
Yikes, I'd just create a new function at this point.
n

nil2l

10/18/2017, 9:31 AM
But… it all looks strange and buggy. In Kotlin I mean.
k

karelpeeters

10/18/2017, 9:49 AM
Just go with the infix one.
n

nil2l

10/18/2017, 11:00 AM
With concat same thing. So it’s not a problem of plus operator.
k

karelpeeters

10/18/2017, 11:00 AM
Right, but the problem is that
+
is defined to combine two lists by the stdlib, so basically you're breaking it.
n

nil2l

10/18/2017, 11:02 AM
Well… if you write
1 + listOf(2)
you receive
undefined
. So what does it break?
k

karelpeeters

10/18/2017, 11:03 AM
The problem is that you break
listOf(1) + listOf(2)
as well.
n

nil2l

10/18/2017, 11:04 AM
Yes. I think it’s a bug in kotlin. Because List< T> != T.
For example if you define concat with
Int
instead of generic
T
it won’t break concat of two lists, it will say type error. And it’s correct.
e

edwardwongtl

10/18/2017, 11:07 AM
I don't think its a bug,
T
is just a over generalised type, if you don't give any bound to it, it can be anything
👍 1
k

karelpeeters

10/18/2017, 11:07 AM
Well no, the compiler just picks
T = Any
and then they both match.
e

edwardwongtl

10/18/2017, 11:08 AM
Make your
T
more specific to the type your want to affect
n

nil2l

10/18/2017, 11:08 AM
Ok. How should I define T to succeed
List< T> != T
?
e

edwardwongtl

10/18/2017, 11:09 AM
Maybe
T: List<*>
?
k

karelpeeters

10/18/2017, 11:09 AM
Well that's the point, it should work for everything but Iterable, and I don't think that's possible.
n

nil2l

10/18/2017, 11:12 AM
Also Collection defines a plus operator. But as I said it’s not only a problem of plus operator.
I’ll try more later. Thanks for suggestions. 🙂
k

karelpeeters

10/18/2017, 11:28 AM
Collection defines a more specific method that can be more performant than the iterator one in some cases, but that's not a problem because the bahavior is exactly the same.
2 Views