why isn't this implicitly forced?
# getting-started
m
why isn't this implicitly forced?
e
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
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
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
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
nope
it may be safe but that is only known outside of the type system