Tegiri Nenashi
Tegiri Nenashi

Reputation: 3086

spread operator function call ambiguity

When converting java StringTokenizer to kotlin split I'm unable to supply a list of character delimiters:

val delim : Array<Char> = arrayOf('(',')','{','}','[',/*many more...*/)
sourceExpr.split(delimiters=*delim,ignoreCase=false,limit=0)

Here kotlin compiler for some reason is unable to disambiguate between split(vararg String,...) and split(vararg Char,...). Questions:

  1. Is is a bug?
  2. Is there cast workaround?
  3. How did ancient pre-Collection era vararg concept infiltrated modern programming language?

Upvotes: 3

Views: 76

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134714

The issue isn't disambiguation but rather an incorrect type. In Kotlin, an Array<Char> is equivalent to a Java Character[]. That means that when you use *delim, you're actually creating a vararg Character (instead of vararg Char).

Instead, you should prefer the primitive-specific CharArray:

val delim: CharArray = charArrayOf('(', ')', '{', '}')
sourceExpr.split(delimiters = *delim, ignoreCase = false, limit = 0)

Upvotes: 6

Related Questions