-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible fix for unnecessary casts to KProperty1 inside validatableOf calls #53
Comments
Yeah, I'm the author of both issues, just forgot to update the comments 😅
The issue is with nullable validatables, did you manage to make this example work? Validator<String?> {
validatableOf(String::length)
} Note that those methods shouldn't be used in userland at the moment, only the accessors generated by the KSP plugin should use them. I might have to put them behind an opt-in requirement. Since they shouldn't be used in userland, I don't think the cast is really an issue? |
Oh, I notice you're the author only in second one 😁
I mean, looks like both tests I mention before are checking exactly this - nullable chaining scenaries and both tests were green after changes. Just to be sure I pasted your example into this method and compiler looked pretty happy: Right now linter is liying to me about those casts: But without this cast I get error during build:
When changing method like this Cast become unnecessary, tests green and nothing seems to explode.
Right now methods are available. If you really decide to restrict access to those, some users most likely would pretty upset about it. And even in case this method be internal, this cast is adding several completely pointless bytecode operations to every invocation. |
Can you please try again with added annotation? @JvmName("nullableValidatableOfProperty")
@JsName("nullableValidatableOfProperty") // <-- THIS ONE
public fun <T : Any, V> Validatable<T?>.validatableOf(getter: KProperty1<T, V>): Validatable<V?> {
return Validatable(unwrap()?.let { getter.get(it) }, getter.name, this)
} |
Seems to work, that's good news. Thank you for the insight! However, I have some doubts on the reasons that led me to use definitely non-nullable types, I'm a bit afraid I might break something I'm not aware of now that the code is written. (I should have add more comments 😅 ) This issue doesn't seem to be a priority for the moment. Let's keep it open but I want to postpone it for the day I'll work on the K2 compiler plugin, those are related. |
Yeah, I'm also worrying this change could somehow break something for none-jvm users, so absolutely agree with your resolution |
I notice there are several places where library code have difficulties using definitely non-nullable types:
akkurate/akkurate-core/src/commonTest/kotlin/dev/nesk/akkurate/validatables/ValidatableTest.kt
Line 106 in 492b847
akkurate/akkurate-core/src/commonTest/kotlin/dev/nesk/akkurate/validatables/ValidatableTest.kt
Line 118 in 492b847
akkurate/akkurate-ksp-plugin/src/main/kotlin/dev/nesk/akkurate/ksp/ValidateAnnotationProcessor.kt
Line 229 in 492b847
After checking referenced kotlin issue, it seems it is not an actual root of this problem. Actual referenced issue should be this one.
Looking at this second issue right now my guess it will stay in backlog for a very long time. And it is definitely not fixed in 1.9.20 and even in 2.0.21 (I checked).
I think this could actually be fixed internally just by using nullable type bounds instead of definitely non-nullable type on one method.
Instead of
we can do
I already tried this change and everything seems to build/run/work without any problem and with
KProperty1
casts removed.After checking kotlin documentation I feel there is no need for definitely non-nullable type here. Some quotes:
The text was updated successfully, but these errors were encountered: