> I wonder if there’s a way to prevent `isA` i...
# strikt
r
I wonder if there’s a way to prevent
isA
if the generic type of the function has its own generic type
I don't think there is a way to prevent it compile time, but you can always just half-botch it by having the assert always fail when
T::class.typeParameters
is non-empty (eg, the reified class has some generic parameters on it's own)
c
maybe you could even trigger the collection type checking automatically that way
r
I don't think you can get what's actually in the type parameter, unless I'm missing something
Like, you get list of KTypeParameter, which is a sibling of KClass (Both inherit from https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-classifier.html)
And the stuff like "name" is what's declared in class. For example:
Copy code
inline fun <reified T> test() = T::class.typeParameters.map { it.name }

test<Collection<String>>()
gives you
["E"]
r
yeah, that’s a neat idea
e
yeah, having the assertion always fail if the subject has another level of generic parameters would be nice
r
though, you would want stuff like
isA<List<*>>()
to work tho 🤔 can you distinguish between those two? I don't think you do even that...
r
if it’s reified I think you should be able to tell it’s using a wildcard
e
doesn’t look like it. that map function up there ^ returns
[E]
for
*
as well
r
sorry, I’m neck deep in SQL explain plans at work
e
it looks like
variance
is always
out
,
name
is always
[E]
and
upperBounds
is always
kotlin.Any?
c
the E comes from the declaration (Collection<E>)
👍 1
maybe we need some kind of
isAWildcard
that allows wildcards
its really an exception and catching the isA<X<Y>> case is more important imo
e
unrelated, but why do kotlin scratches just suddenly stop working w/ no sort of message? i end up scrapping the scratch and just putting it in a test most of the time
c
I always write tests, its a habit from before scratch files existed
e
isA
could have a
ignoreGenerics: Boolean = false
parameter for when you want
isA<List<*>>(ignoreGenerics = true)
to pass, although that feels a little clunky
r
very easy to forget as well
c
the error message could mention it
☝️ 1
isA just does not work like expected and people write asserts that don’t check what they think they do. the other case can just be documented
e
there’s also precedent on breaking tests on bad assertions, like the relatively recent changes on the map stuff, where it detected there wasn’t a proper assertion. we had to refactor a few tests when we upgraded to that version. we also realized that in several tests we really hadn’t been testing anything at all. this is similar
c
the nested isA is something that we talked about at work some weeks ago. everybody was very suprised
e
maybe could write a custom detekt rule?
c
its also not at all strikt specific, we had the problem in tests with strikt matchers but also in older tests that use kotest matchers