0

I would like to use something like the following, which does not compile:

value class PositiveDecimal(val d: BigDecimal): BigDecimal by d {
    init {
        require(d > BigDecimal.ZERO) { "Value must be positive, was: $d" }
    }
}

so that I can use my positive decimal without unboxing, like this:

val length = PositiveDecimal(BigDecimal.ONE)
if(length < maxLength) {
...
(snip)

is there a way to have all the public methods/properties of the embedded value d exposed by delegation?

AlexC
  • 3,343
  • 6
  • 29
  • 38
  • 1
    I'm afraid you can't use any Kotlin syntactic sugar for that because delegation only works with interfaces, and `BigDecimal` is just a regular class. I think you have to manually delegate the various methods you want to expose – user2340612 Mar 10 '23 at 16:06

0 Answers0