My naive implementation ```operator fun <T> ...
# announcements
e
My naive implementation
Copy code
operator fun <T> T.plus(list: List<T>): List<T> {
  val result = list.toMutableList()
  result.add(0, this)
  return result
}
b
i'd change the argument to MutableList<T> to avoid implicit conversion and require end-user to convert the list themselves
Immutable list should be left immutable 😄
👍 1
g
Agree with @Big Chungus. Plus I would personally make the extension on list instead of
T
to avoid polluting the namespace of pretty much everything. This will require to do
mutableListOf<Int>() + 2
instead of
2 + mutableListOf<Int>()
e
@Big Chungus that is not the case for the standard library plus operator. You can do
listOf(1) + 2
@Giorgos Neokleous The whole point with this proposal was to have item as the first item in the new list
👍 1
@Big Chungus BTW
toMutableList()
will create a separate list with copy of the original, so original will stay without changes.
@Giorgos Neokleous ☝️
n
The implementation is really inefficient, btw
😁 1
Better to just do listOf(this) + list, one liner yet still more efficient than current
m
Agreed, it will be encapsulated by the extension method anyway
e
You mean that inserting element to the start has to shift all other elements again. It was naive implementation, I can for sure create new ArrayList of list size + 1 and do further elements placement. The main question was do you feel that this function is need?