Any idea while the compiler isn’t able to infer that the
self-referential
vararg parameter passed on the constructor is a container and thus the given class is
constructible
?
Current fix: Requires I make the parameter type nullable or otherwise suppress this warning.
d
Dominaezzz
01/27/2020, 9:26 AM
Does
class GraphNode(val value: Int, val children: Array<GraphNode>)
work?
c
charlesmuchene
01/27/2020, 9:29 AM
No it doesn’t. That requires me to pass an array when constructing every node or otherwise an empty array. Yikes!
charlesmuchene
01/27/2020, 9:35 AM
There are several ways to go about this implementation. I was just wondering why the compiler isn’t able to infer that a vararg will result in a container type that can be empty.
charlesmuchene
01/27/2020, 9:40 AM
@Dominaezzz I could modify your suggestion to this:
Copy code
class GraphNode(val value: Int, children: Array<GraphNode> = emptyArray())
d
dmitriy.novozhilov
01/27/2020, 10:13 AM
SelfReferenceConstructorParameter
is an IDE warning, not compiler's (and it doesn't affect compilation)
Look like there is a bug in corresponding inspection, so you can create a ticket at https://youtrack.jetbrains.com/issues/KT
c
charlesmuchene
01/27/2020, 12:21 PM
Thanks for the clarification @dmitriy.novozhilov. Creating a ticket on this.