Ryan Sang
12/22/2023, 9:08 PM@JvmInline
value class Password private constructor(val password: String) {
companion object {
@JvmStatic
@JvmName("of")
fun of(password: String) = Password(password)
}
}
and a corresponding java class
public class JavaClass {
public void func(Password word) {
System.out.println(word);
}
public void invoke() {
String s = "test";
this.func(Password.of(s));
}
}
I get an error on the invocation of this.func
claiming that the required type is Password and I have provided String. Is there any work around? Or can value classes not be used as parameters for java methods?ephemient
12/22/2023, 9:33 PMString
at runtime (except when boxed)Stephan Schröder
12/22/2023, 9:33 PMpublic void func(Password... words) {
if(words.length!=1){throw new IllegalArgumentException();}
var word = words[0];
System.out.println(word);
}
since arrays actually retain their generic type.ephemient
12/22/2023, 9:34 PMephemient
12/22/2023, 9:39 PM