Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 321
Tegiri Nenashi Avatar asked Mar 30 '26 18:03

Tegiri Nenashi


1 Answers

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)
like image 79
Kevin Coppock Avatar answered Apr 02 '26 21:04

Kevin Coppock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!