Mark
02/23/2022, 8:01 AM/**
* usage: val regex by "foo".lazyRegex(RegexOption.IGNORE_CASE)
* instead of:
* val regex by lazy { "foo".toRegex(RegexOption.IGNORE_CASE) }
* or:
* val regex = "foo".toRegex(RegexOption.IGNORE_CASE)
*/
fun String.lazyRegex(option: RegexOption? = null): Lazy<Regex> =
lazy {
if (option == null)
toRegex()
else
toRegex(option)
}
So here, if you already have a bunch of properties like:
val regex = "foo".toRegex()
you can very easily convert to:
val regex by "foo".lazyRegex()
Paul Woitaschek
02/23/2022, 8:43 AMMark
02/23/2022, 8:52 AMRoukanken
02/23/2022, 9:03 AM+ kotlinParameter().ofFunction(0, kotlinFunction().withName("Regex").definedInClass("kotlin.text.Regex"))
+ receiver().ofFunction(kotlinFunction().withName("toRegex").withReceiver("kotlin.String").definedInPackage("kotlin.text"))
+ receiver().ofFunction(kotlinFunction().withName("toPattern").withReceiver("kotlin.String").definedInPackage("kotlin.text"))
in "Places patterns"+ receiver().ofFunction(kotlinFunction().withName("toLazyRegex").withReceiver("kotlin.String").definedInPackage("......"))
Mark
02/23/2022, 9:07 AMRoukanken
02/23/2022, 9:08 AMMark
02/23/2022, 9:10 AMPaul Woitaschek
02/23/2022, 9:14 AMMark
02/23/2022, 9:26 AM.toRegex()
and .toRegex(RegexOption)
during startup? I have about 1000+ Regex properties and some of the patterns are very complex, so I’d be interested to measure anyway.Paul Woitaschek
02/23/2022, 9:29 AMMark
02/23/2022, 9:31 AMPaul Woitaschek
02/23/2022, 9:32 AMMark
02/23/2022, 9:35 AMPaul Woitaschek
02/23/2022, 9:36 AMMark
02/23/2022, 9:37 AMPaul Woitaschek
02/23/2022, 9:39 AMephemient
02/23/2022, 9:53 AMMark
02/23/2022, 11:14 AMPaul Woitaschek
02/23/2022, 11:22 AMMark
02/23/2022, 11:26 AMPaul Woitaschek
02/23/2022, 11:27 AMMark
02/23/2022, 11:30 AMby lazy
regex property on the enum class.Paul Woitaschek
02/23/2022, 11:31 AMAdam Powell
02/23/2022, 4:25 PMPaul Woitaschek
02/23/2022, 4:26 PMMark
02/24/2022, 12:59 AMephemient
02/24/2022, 1:07 AMlazy
, except that the JVM handles synchronization on initialization, doesn't require synchronization on subsequent accesses, and doesn't require Kotlin to generate all the KProperty/delegate stuff.Mark
02/24/2022, 1:09 AMlazy
than one might originally have thought.ephemient
02/24/2022, 1:18 AM@file:JvmMultifileClass
)Mark
02/24/2022, 1:27 AMephemient
02/24/2022, 1:28 AMMark
02/24/2022, 2:04 AMfooMap
.keys
.joinToString(prefix = "[", separator = "", postfix = "]")
.toRegex()
Would you default to 1️⃣ lazy or 2️⃣ standard property?