Skip to content

Possible fix for unnecessary casts to KProperty1 inside validatableOf calls #53

@bejibx

Description

@bejibx

I notice there are several places where library code have difficulties using definitely non-nullable types:

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

@JvmName("nullableValidatableOfProperty")
public fun <T : Any?, V> Validatable<T>.validatableOf(getter: KProperty1<T & Any, V>): Validatable<V?> {
    return Validatable(unwrap()?.let { getter.get(it) }, getter.name, this)
}

we can do

@JvmName("nullableValidatableOfProperty")
public fun <T : Any, V> Validatable<T?>.validatableOf(getter: KProperty1<T, V>): Validatable<V?> {
    return Validatable(unwrap()?.let { getter.get(it) }, getter.name, this)
}

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 most common use case for declaring definitely non-nullable types is when you want to override a Java method that contains @NotNull as an argument. For example, consider the load() method

When working only with Kotlin, it's unlikely that you will need to declare definitely non-nullable types explicitly because Kotlin's type inference takes care of this for you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DSLAPI design and its DSLcoreThe core of the library is involvedenhancementNew feature or requestksp-pluginThe KSP plugin is involved

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions