Matt Nelson
04/30/2022, 11:30 AMsealed interface Key {
fun descriptor(address: Address): String
}
@JvmInline
value class Address(@JvmField val value: String)
Calling From Java:
public class SomeClass {
public void someFunction(Key key) {
// Key.descriptor accepts a String, not an Address
String descriptor = key.descriptor("");
Address address = new Address("someaddress");
// key.descriptor does not accept an Address and IDE shows error
String descriptor2 = key.descriptor(address);
}
}
Any Ideas?George
04/30/2022, 3:47 PMmike.holler
05/02/2022, 2:59 PMSomeClass().someFunction("someaddress")
-- value types are not enforced by the JVMMatt Nelson
05/02/2022, 9:21 PMsealed interface Port {
val value: Int
companion object {
@JvmStatic
@Throws(IllegalArgumentException::class)
operator fun invoke(port: Int): Port {
return RealPort(port)
}
}
}
@JvmInline
private value class RealPort(override val value: Int): Port {
init {
require(value in 0..65535) {
"Invalid port range"
}
}
}
And calling from Java:
Port port = Port.invoke(65536); // Will throw IllegalArgumentException
This way, all my interfaces that take a Port
now compile to the sealed interface, instead of the primitive type.mike.holler
05/02/2022, 9:23 PMdata class Port(val value: Int)
?data class Port(...)
would have the same affect I'd think.Matt Nelson
05/02/2022, 9:26 PMmike.holler
05/02/2022, 9:26 PMMatt Nelson
05/02/2022, 9:26 PMmike.holler
05/02/2022, 9:27 PMMatt Nelson
05/02/2022, 9:27 PMmike.holler
05/02/2022, 9:27 PMMatt Nelson
05/02/2022, 9:32 PMmike.holler
05/02/2022, 9:33 PMMatt Nelson
05/02/2022, 9:34 PMmike.holler
05/02/2022, 9:35 PMtypealias
if you're feeling curious.Matt Nelson
05/02/2022, 9:41 PMmike.holler
05/06/2022, 10:24 PMWrappedLong
implementation that is compatible with JS but performs very quickly on JVM. You are a genius.Matt Nelson
01/11/2023, 2:01 PMvalue class
not compiling to platform code.
https://github.com/05nelsonm/component-value-clazz