v79
01/06/2023, 5:26 PMdata class Name(val title: String, val lowerTitle = title.lowercase())
? I.e. a default value dependent on the value of another field? It seems to compile...Jacob
01/06/2023, 5:32 PMName("My Title", "TOTALLY NOT MY TITLE Or LOWER")
Jacob
01/06/2023, 5:35 PMv79
01/06/2023, 5:36 PMJacob
01/06/2023, 5:37 PM.copy
will break the relationship between these propertiesv79
01/06/2023, 5:40 PM@Serializable
data class PostMetadata(val title: String, val template: String = "post", val slug: String = title.toSlug(), val date: LocalDate)
// and String extension function 'toSlug()'...
fun String.toSlug() : String {
val reserved = "[;/?:@&=+\$, ]"
return this.replace(Regex(pattern = reserved),"-").lowercase()
}
I do feel I'm abusing power here.Rob Elliot
01/06/2023, 6:00 PMSlug
class (as a value class
if that works OK for you) and enforce the invariant in its constructor.v79
01/06/2023, 6:01 PMRob Elliot
01/06/2023, 6:04 PMRob Elliot
01/06/2023, 6:06 PMvalue class Slug
only exists at compile time, at runtime it will just be a String and the constructor will just be a static method that calls require(slug.matches(legitimate))
.Rob Elliot
01/06/2023, 6:06 PMSlug
instance at runtime - but I can't remember when that happens...)