Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Regex thread-safety

In Java, Pattern class is thread-safe all right. But is Kotlin Regex (created like "[ABC]+".toRegex()) thread-safe on the JVM and other runtimes?

like image 666
jihor Avatar asked Jul 05 '19 15:07

jihor


2 Answers

The documentation for Regex points you to java.util.regex.Pattern for the JVM.  So it will inherit Pattern's concurrency behaviour.

(Note that checking the current implementation isn't enough; JetBrains can and do change their implementations.  But if it's specified in the docs, then it's part of the public API and should be fairly reliable.)

The corresponding docs for JavaScript point here, which doesn't mention anything about thread-safety.  And that for Native doesn't even have a link.  So it's probably not safe to assume anything about them.

like image 187
gidds Avatar answered Oct 03 '22 13:10

gidds


In Kotlin toRegex is just an extension function that creates a Regex object. On Kotlin/JVM that just calls Pattern.compile(pattern), so the underlying code comes from the JDK and is thread-safe.

like image 31
user2340612 Avatar answered Oct 03 '22 12:10

user2340612