David Kubecka
02/27/2023, 4:27 PM==
, >
, etc.? For "normal" functions I would use ::
but not sure how to do that for these operators.
I want to convert user input (as string) to the operator function and then call this function on the provided values.Landry Norris
02/27/2023, 4:29 PM1::plus
works if you specify which plus. It does require that the operator is defined for the type.Riccardo Lippolis
02/27/2023, 4:29 PM==
-> equals
>
-> compareTo
Landry Norris
02/27/2023, 4:29 PMRiccardo Lippolis
02/27/2023, 4:30 PMgo to implementation
feature to look this up via your IDE)David Kubecka
02/27/2023, 4:34 PM::equals
but that gives me only a single parameter function.Luke Armitage
02/27/2023, 4:36 PM::equals
is a single argument function - it compares the receiver to the argument. 😄Riccardo Lippolis
02/27/2023, 4:37 PMval instanceEquals: KFunction1<Any?, Boolean> = "My string"::equals
val stringEquals: KFunction2<String, Any?, Boolean> = String::equals
Landry Norris
02/27/2023, 4:37 PMT.equals(Any)
is the general signature for the equals method, where in this case T means a type.David Kubecka
02/27/2023, 4:46 PM::
, right? Instead, I would have to do something like { x: T, y: T -> x.compareTo(y) > 0 }
Landry Norris
02/27/2023, 4:46 PMDavid Kubecka
02/27/2023, 4:47 PMLandry Norris
02/27/2023, 4:48 PMLandry Norris
02/27/2023, 4:49 PMT.compareToAsBoolean(y: T) -> Boolean
, but with better #namingDavid Kubecka
02/27/2023, 5:05 PMval valueCompareTo = it.value.toBigDecimal()::compareTo
return when (it.operator) {
FilterOperator.EQUAL -> { money: MoneyDto -> valueCompareTo(money.amount) == 0 }
FilterOperator.GREATER_THAN -> { money: MoneyDto -> valueCompareTo(money.amount) > 0 }
FilterOperator.GREATER_THAN_OR_EQUAL -> { money: MoneyDto -> valueCompareTo(money.amount) >= 0 }
FilterOperator.LESS_THAN -> { money: MoneyDto -> valueCompareTo(money.amount) < 0 }
FilterOperator.LESS_THAN_OR_EQUAL -> { money: MoneyDto -> valueCompareTo(money.amount) <= 0 }
else -> error("operator ${it.operator} not supported here")
}
Ideally, I would like to return only the operator from the when expression and inject that into the money lambda (defined at single place only), but since there's no such thing as a comparison function that returns boolean I have to repeat that code. Or... ?Riccardo Lippolis
02/27/2023, 5:09 PMisGreaterThan
needs to be defined somewhere, in that case. You can choose to put it in the when
expression, in the MoneyDto
(if you have control over it), or define it as an extension function, for exampleDavid Kubecka
02/27/2023, 5:18 PMfun <T> isGreaterThan(a: T, b: T) = a.compareTo(b) > 0
seems to be quite generic 🙂David Kubecka
02/27/2023, 5:21 PMLandry Norris
02/27/2023, 5:22 PMLandry Norris
02/27/2023, 5:23 PMfun <T: Comparable<T>> isGreaterThan(a: T, b: T) = a.compareTo(b) > 0
Landry Norris
02/27/2023, 5:23 PMDavid Kubecka
02/27/2023, 5:25 PMLandry Norris
02/27/2023, 5:27 PMLandry Norris
02/27/2023, 5:30 PMDavid Kubecka
02/27/2023, 5:30 PMDavid Kubecka
02/27/2023, 5:32 PMLandry Norris
02/27/2023, 5:35 PMLandry Norris
02/27/2023, 5:36 PMDavid Kubecka
02/27/2023, 5:41 PMval filteredValue = it.value.toBigDecimal()
val cmp = filteredValue::isEqualTo
Apparently, that's not how it's supposed to be...Landry Norris
02/27/2023, 5:42 PMfoo::isEqualTo
, you have to make sure the signature is T.isEqualTo(other: T) -> Boolean
. If it’s like your greater than method above, you defined isEqualTo(x: T, y: T) -> Boolean
, which is different.David Kubecka
02/27/2023, 5:42 PMLandry Norris
02/27/2023, 5:43 PM::isEqualTo
David Kubecka
02/27/2023, 5:44 PM::isEqualTo
and assign it to val but that's not possible without specifying the T
. Will consider the other option (extension functions)David Kubecka
02/27/2023, 5:46 PMLandry Norris
02/27/2023, 5:47 PMDavid Kubecka
02/27/2023, 5:49 PMisEqualTo
to single arg isEqualTo
(with fixed first arg).