CLOVIS
06/18/2025, 8:04 AMclass Foo<out T>
I need to create a function
fun <T> Foo<T>.foo(o: Foo<T>)
that function should NOT compile if the two T
are different. As it is, it does compile because T
is inferred to Any?
.
Currently, I use @OnlyInputTypes
, but that's very brittle and I'd prefer to migrate to something more stable.
I have tried
fun <T, R : T> Foo<T>.foo(o: Foo<R>)
but T
also gets inferred to Any?
.
Is there another way to declare that constraint?Stephan Schröder
06/18/2025, 12:41 PMinline fun <reified T> Foo<T>.foo(o: Foo<T>)
helps, so I guess there's no way to circumvent the type earasure 🤔
Playground: https://pl.kotl.in/fu_l-9sUZ
I guess you could write a compiler plugin or maybe even KSP is powerful enough.Adam S
06/18/2025, 2:02 PMCLOVIS
06/18/2025, 2:03 PMCLOVIS
06/18/2025, 2:03 PM@OnlyInputTypes
doesn't either anywayCLOVIS
06/18/2025, 2:03 PMfoo()
a member of Foo
(instead of an extension), but that is not feasible in my caseAdam S
06/18/2025, 2:13 PM@kotlin.internal.Exact
triggers "Argument type mismatch" error
https://pl.kotl.in/G3bbKPUjvCLOVIS
06/18/2025, 2:31 PM@OnlyInputTypes
and @Exact
?CLOVIS
06/18/2025, 2:31 PM@OnlyInputTypes
because that's what assertEquals
usesCLOVIS
06/18/2025, 2:33 PMCLOVIS
06/18/2025, 2:33 PM@Exact
, maybe I should migrate to it 🤔CLOVIS
06/18/2025, 2:37 PM@Exact
, because it refuses subtypes of T
, which @OnlyInputTypes
doesn'tYoussef Shoaib [MOD]
06/18/2025, 3:56 PM@OnlyInputTypes
that brittle? It feels safer to me than a compiler plugin. Also, if it ever doesn't work, then it's as if it wasn't there.
I'm talking btw about shadowing it in your own module so that you have access to it, not on about error suppression (which is a bad idea)CLOVIS
06/18/2025, 8:46 PMIsI've had issues before when it broke during an upgrade.that brittle? It feels safer to me than a compiler plugin.@OnlyInputTypes
I'm talking btw about shadowing it in your own module so that you have access to it,That doesn't work anymore. I think it broke with K2, I don't remember.
not on about error suppression (which is a bad idea)…I knoooow… 😕