Hi! I’m refactoring some code at work and started...
# getting-started
v
Hi! I’m refactoring some code at work and started looking at inline or value classes as a way to create a more detailed domain model. I do have some concerns about Java compatibility. Here is an example where I have an identifier that is used in my domain model.
Copy code
public interface Identifier {
    public val value: String
}

private val validationRegex = Regex("""^[0-9a-f]{32}$""")

@JvmInline
value class SampleIdentifier(override val value: String) : Identifier {
    init {
        require(validationRegex.matches(value)) {
            "The value $value is not a valid SampleIdentifier"
        }
    }
}

class Sample(
    val identifier: SampleIdentifier,
) {
}
This works wonderfully in Kotlin, but I can’t figure out how someone using Java (we still have a lot of that) could create an instance of Sample. The following code fails saying that the constructor has private access:
Copy code
Sample sample = new Sample("1295f");
I’m assuming this is due to function name mangling but am not sure how to integrate it with existing Java code. Thanks!