https://kotlinlang.org logo
Title
a

adam-mcneilly

04/05/2018, 9:29 PM
I want to write a test that checks if a property I have is a certain type. The property is a sealed class, so I want to write something like:
fun assertNetworkStateType(clazz: Class<*>) { assertTrue(networkState is clazz) }
so that I can call it like
assertNetworkStateType(NetworkState.Loading)
but I can't quite figure out what the type of that parameter would be.
d

diesieben07

04/05/2018, 9:38 PM
If I understand you might want an inline function with a
reified
type parameter:
inline fun <reified T> assertNetworkStateType(obj: Any?) = assertTrue(obj is T)
a

adam-mcneilly

04/05/2018, 9:41 PM
and how would I call that?
ah I see
assertNetworkStateType<NetworkState.Loading>()
works. Thank you. 🙂
I'm all the sudden less convinced this is the way I really want to do it, but at least now I know if I ever come across a more appropriate use case.
d

diesieben07

04/05/2018, 9:49 PM
Then you should explain what exactly you want 😉