https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

Michael de Kaste

06/21/2022, 1:15 PM
why isn't this implicitly forced?
e

ephemient

06/21/2022, 2:20 PM
1. the compiler is pretty limited in what it tracks between variables for smart-casting, e.g. https://kotlinlang.org/spec/type-inference.html#bound-smart-casts 2. are you intentionally relying on invariance? if the declaration were
Type<out T>
then
doSomethingWithType<Any>(Type.OnlyWithString(""), Holder(0))
would be completely legal
1
m

Michael de Kaste

06/21/2022, 2:37 PM
Well the actual Type as is in our application is covariant (
out T
), but in general, I only want to be able to call doSomethingWithType if both Type and Holder have the exact same types
e

ephemient

06/21/2022, 2:42 PM
then you can't check that they're both the same without something like
data class HolderWithType(val type: String, val value: String) : Type<String>, Holder<String>
and using that instead
m

Michael de Kaste

06/21/2022, 2:52 PM
the use-case is, we're building search-criterias for our database where we have a searchCriteriaType (
IsLessThan<T : Comparable<T>>
,
IsNull<Nothing>
, etc.) and a path source from javax. For some of the criteriabuilder functions like the actual "greaterThan", the types of the path and expression need to have the same subtype. But for a
like
function, path needs to be string, so we setup a searchcriteria that only accepts strings as well
but no matter how I implement that, it appears I can't make it "type-safe"
e

ephemient

06/21/2022, 2:54 PM
nope
it may be safe but that is only known outside of the type system