This is disappointing. So if I inline an `Int` tha...
# random
t
This is disappointing. So if I inline an
Int
that is a factor of
5
, I can't enforce this due to private constructor not being allowed?
h
Ya, it’s because of java interop
Wish it could cause then you could create something like Vector3(xyz: DoubleArray) and verify that xyz was actually length 3
t
Yeah... to me this is the whole reason to use inline classes: creating a compiler-restricted set of primitive values that are still primitives. It's really a bummer.
r
Was going to say that an
init
block might do the trick, but that's also not allowed for inline classes... That's disappointing indeed
l
You can't enforce it yet. This is something they are working on.
h
@thomasnield Looking at
UByte
I noticed they mark the constructor as internal, which isn't allowed normally, but is enabled via
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
😮 1
Which allows you to do this:
Copy code
@Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
inline class FactorOfFive internal constructor(internal val value: Int)
With a "construction extension" like this:
Copy code
fun FactorOfFive(value: Int) = when {
    value % 5 == 0 -> FactorOfFive(value)
    else -> throw IllegalArgumentException("Value is not a factor of 5")
}
👏 1
t
@hudsonb cool, so it looks like something is in the works and already in experimental stages