benleggiero
03/14/2018, 3:48 PMbenleggiero
03/17/2018, 6:27 PMorangy
val a5 = [] // Compiler error: type cannot be inferred. See below
It can be List<Nothing>
may be?jkbbwr
03/18/2018, 12:22 PMoperator fun listLiteral(varargs args) = listOf(*args)
jkbbwr
03/18/2018, 12:22 PMjkbbwr
03/18/2018, 12:24 PMlistOf
and similar I havent found myself missing literal syntaxorangy
mapOf
for each item, or classname for each item, etc. Literal syntax is really useful when there are 3 things playing together:
* list literal
* map literal
* class literal
and these things integrated into a single initialization construct:
class ServerConfig(connectors: List<Connector>, properties: Map<String, Any>)
class Connector(protocol : HttpProtocol, host : String, port : Int)
val config = ServerConfig( // normal primary constructor
connectors = [ // list literal
(protocol = HTTP, host = "<http://foo.com|foo.com>", port = 80), // class literal, type is inferred from element of list
(protocol = HTTPS, host = "<http://foo.com|foo.com>", port = 443)
],
properties = [ // map literal
"debug" = true,
"logLevel" = INFO,
]
)
(this is just my own opinion and not something designed or considered for the language at the moment)gildor
03/18/2018, 2:32 PMbenleggiero
03/18/2018, 2:58 PMbenleggiero
03/18/2018, 3:00 PMdeviant
03/19/2018, 3:39 PMoperator
is more suitable for this.benleggiero
03/19/2018, 4:36 PMdeviant
03/19/2018, 4:38 PMbenleggiero
03/19/2018, 4:39 PMbenleggiero
04/06/2018, 2:11 AMraulraja
04/06/2018, 12:44 PMlistOf
then it would only be usable in certain scenarios and you'll have to resort back to function factories in cases for example when you want the underlying data type to be something like ImmutableList
benleggiero
04/07/2018, 3:01 AMbenleggiero
04/07/2018, 4:04 AMtschuchort
04/07/2018, 3:00 PMval list = <Any>[1,2,3]
val list2 = <Int>[]
benleggiero
04/07/2018, 3:22 PMtschuchort
04/07/2018, 3:31 PMfun <T> foo(l: List<T>): List<T>
val l2 = foo([1,2,3]) //infered type is List<Int>
l2 += "hello2" // we actually wanted List<Any>
in many cases this can be solved by specifying the generic type of foo
explicitly, but what if foo
is parameterized over many possibly complex or even anonymous types?
<T>[]
is equivalent to listOf<T>()
benleggiero
04/07/2018, 3:34 PMfun <T> foo(l: List<T>): List<T>
val l2: List<Any> = foo([1,2,3]) // explicit type is List<Any>
l2 += "hello2" // we actually get and use List<Any>
You may also do this:
val l3: = foo([1,2,3]) as List<Any> // explicit type is List<Any>
l3 += "hello2" // we actually get and use List<Any>
tschuchort
04/07/2018, 3:41 PMfun foo() = object {
var x = 1
var y = 2
}
fun <S,T,V> Observable<V>.bar(a: List<T>, b: S): Observable<List<T>>
Observable.just(Unit)
.bar([1,2,3], foo())
.map { it += "hello" } // error infered type is Observable<List<Int>> but we want Observable<List<Any>>
tschuchort
04/07/2018, 3:44 PMbar
is not feasible since S
is the anonymous return type of foo
(Or may just be really long in a different example)tschuchort
04/07/2018, 3:45 PM[1,2,3]
to List<Any>
but I think it would be cleaner to have syntax for this instead of castingtschuchort
04/07/2018, 3:47 PMList<Int>
and then cast to List<Any>
. In the latter case this would lead to issues when we are dealing with custom collection types:
[1,2,3] as MyList<Any> // illegal cast
tschuchort
04/07/2018, 3:48 PMMyList.<Any>[1,2,3]
analog to MyList.listOf<Any>(1,2,3)
benleggiero
04/07/2018, 4:09 PMas
...
Observable.just(Unit)
.bar([1,2,3] as List<Any>, foo())
.map { it += "hello" } // error infered type is Observable<List<Int>> but we want Observable<List<Any>>
benleggiero
04/07/2018, 4:14 PMas
tells the compiler the initial type of the sequence. It's not casting anything.raulraja
04/07/2018, 7:32 PMval x = [1, 2, 3]
work. In your proposed solution you have to type the receiver always. Not saying that it's not acceptable, I think it'd be a fair compromise to support multiple collection types that have similar semantics as List
benleggiero
04/07/2018, 9:43 PMList
is the superior default sequence type. That combined with the fact that they can improve the performance and safety of the backing class while keeping the interfaces exactly the same make it a very good default type.
I don't necessarily agree, but it's good enough and has enough support behind it that I'm not willing to argue against it in this proposal, especially given the amount of usefulness that I think this proposal will bring to the language as a whole.raulraja
04/08/2018, 1:11 AMList
but if one it's specified then it uses the implementation from the particular receiver. That can introduce unexpected runtime behaviors since both may be of List
interface but have different backing implementations and it's not obvious that for example:
val x = [0, 1, 2] // Default list impl
val y : LinkedList<Int> = [0, 1, 2] //LinkedList impl
I'd expect the first one to result in a compiler error expecting it to at least be typed to List
val x: List<Int> = [0, 1, 2, 3] // same as listOf
benleggiero
04/08/2018, 1:52 AMraulraja
04/08/2018, 10:03 PMlistOf
, mutableListOf
, arrayListOf
, arrayOf
... I don't have a counter proposal, I was hoping to discuss yours because I thought it was good but we needed to workout some of the ambiguity to see what the trade off are beside the obvious syntactic enhancement. Since you don't have the energy I don't have much to add. Good luck with the proposal and I hope it makes it through. Looking forward to enhanced syntax for collections. 🍻gildor
04/09/2018, 1:29 AMIt can be@orangy Actually it’s interesting question. I know this proposal + related issues https://youtrack.jetbrains.com/issue/KT-7590 This proposal has “Target versions: 1.3” but in “To be discussed” state, really curious are there any plans about this?may be?List<Nothing>
benleggiero
04/09/2018, 5:51 AMlistOf
, mutableListOf
, mapOf
, and mutableMapOf
all return opaque types masked by their generic interfaces. In fact, as far as I can tell, listOf
and mutableListOf
are generally both backed by the same type (like java.util.ArrayList
on JVM). And their usage is highly encouraged throughout the standard library, with things like .filter
and .map
returning that same opaque List
type.benleggiero
04/09/2018, 5:53 AMgildor
04/09/2018, 7:12 AM