Ahmed Mourad
12/27/2020, 12:14 PMdata class X(val v: String)
If this works X::v
shouldn't this also work X?::v
?Hamza
12/27/2020, 12:34 PMAhmed Mourad
12/27/2020, 12:49 PMX?
type itself (the nullable version of X
), not an instance of it.Ahmed Mourad
12/27/2020, 1:06 PMChrimaeon
12/27/2020, 1:16 PMAhmed Mourad
12/27/2020, 1:49 PMA?
is supposed to be as much of a type as A
, it's just a type that accepts null as an instance of it, which should only matter if I try to get a value of v
by calling get
and passing a receiver.
I'm trying to get KProperty1<A?, String>
, which itself should never throw an NPE.Chrimaeon
12/27/2020, 1:51 PM?
and ::
operator cannot work together as accessing a member reference on a nullable object does not make any sense.Chrimaeon
12/27/2020, 1:52 PMA?
is not a type itself A
is the type and ?
tells that it can be null, which is syntactic sugar in kotlin.Ahmed Mourad
12/27/2020, 2:02 PMA?
is a type, you can defines extension functions with it as a receiver, you can pass it as a type parameter, you can use it as a bound for type parameters, you can define parameters and variables of type A?
https://kotlinlang.org/spec/type-system.html#nullable-types
Chrimaeon
12/27/2020, 2:08 PMIn Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that can not (non-null references). For example, a regular variable of typehttps://kotlinlang.org/docs/reference/null-safety.htmlcan not hold nullString
Chrimaeon
12/27/2020, 2:10 PM::
gives you a reference to a function on a class so the class - not the object - cannot be of an nullable type.Nir
12/27/2020, 4:24 PMNir
12/27/2020, 4:25 PMNir
12/27/2020, 4:25 PMAhmed Mourad
12/27/2020, 5:00 PMA?
would contain the properties declared in class A
, but I can see why that wouldn't be the case.Chrimaeon
12/27/2020, 5:04 PMAhmed Mourad
12/27/2020, 5:10 PMn
is nullable, I need to pass a KProperty1<Nested?, String>
to be able to validate against x
Ahmed Mourad
12/27/2020, 5:11 PM🙈 okay, live with that answer.Sneaky 😂
Chrimaeon
12/27/2020, 6:18 PMNestes?
in a variable declarations means that the variable is of type Nested
and can be null. then there is the ?.
operator which is a safe-call operator on such a nullable variable. there is no ?::
operator as referencing a function/property on a Class
with ::
will give you a reference to the function/property. So the reference is on the class not the declaration with type Nested?
. you'r on
function shoudl not care about the parameter n
of the nullable type I guess.Ahmed Mourad
12/27/2020, 7:17 PM::
operator on the Nested?
type, assuming Nested?
contained the properties of Nested
which doesn't seem like it does.
The problem is, the ValidatorBuilder
of the outer on
has two type params, one is passed from the ConstraintBuilder
and one is inferred from the property passed to on
, so it takes Password
from the ConstraintsBuilder
and Nested?
from the property.
The ValidatorBuilder
of the inner on
takes its first type param from the ValidatorBuilder
of the outer on
which is now Nested?
and its second type param is inferred from the property passed to it which is String
.
Now since the property being passed to the inner on
needs to be of type KProperty<Nested?, String>
, I need to either pass Nested?::x
or somehow tell Kotlin I need to extract the non-nullable version of the type parameter inferred.
You can see the declaration of the inner on
here:
https://github.com/AhmedMourad0/kotlin-validation/blob/nesting_validators/core/src/main/kotlin/dev/ahmedmourad/validation/core/DslBuilders.kt#L[…]86
https://github.com/AhmedMourad0/kotlin-validation/blob/nesting_validators/core/src/main/kotlin/dev/ahmedmourad/validation/core/Descriptors.ktNir
12/27/2020, 7:21 PMNir
12/27/2020, 7:21 PMNir
12/27/2020, 7:21 PMNir
12/27/2020, 7:22 PMAhmed Mourad
12/27/2020, 7:47 PMNir
12/27/2020, 9:03 PMNir
12/27/2020, 9:03 PMNir
12/27/2020, 9:04 PMAhmed Mourad
12/27/2020, 9:19 PMT
is used both as nullable and non-nullable, it's used as non-nullable for the property
parameter, and as nullable for the validation
method parameter.
https://github.com/AhmedMourad0/kotlin-validation/blob/6c401d9ecdc819fc63dee1dc5d7[…]/src/main/kotlin/dev/ahmedmourad/validation/core/DslBuilders.ktAhmed Mourad
12/27/2020, 9:26 PMT
is originally nullable, if it isn't then it should be non-nullable in both locations.