Is * arrayOf () an instruction?

I used the distribution operator when I noticed something strange:

// compiles
val list1 = listOf(1, 2, *(if(0 > 1) arrayOf(3) else arrayOf()))

// does not compile  
val list2 = listOf(1, 2, if(0 > 1) *arrayOf(3) else *arrayOf())

One of the compiler errors is

Pending Expression

  • So is there an *arrayOf()expression?
  • If so, how can it be estimated with listOf()?
+4
source share
1 answer

No, this is neither an instruction nor an expression.

The distribution operator has a special role: it can only modify the semantics of an expression with the type of the array passed as vararg, giving a special argument. It cannot be used independently in other expressions, so the code in which it is used inside the expression ifdoes not compile.

+5

Source: https://habr.com/ru/post/1685333/


All Articles