Hoping to do away with an unchecked cast - any tho...
# codereview
j
Hoping to do away with an unchecked cast - any thoughts?
Copy code
@Suppress("UNCHECKED_CAST")
fun <IdType> createWidgetFactory(cls: KClass<out IdType>): WidgetFactory<IdType> =
    when (cls) {
        IdA::class -> WidgetFactoryA() as WidgetFactory<IdType>
        IdB::class -> WidgetFactoryB() as WidgetFactory<IdType>
        else -> throw UnsupportedOperationException("Unknown WidgetFactory type!")
    }
j
Personally i just keep unchecked cast for these cases. Idk if the typechecker is smart enough to do this 100% type-safe
👍 1
t
Why lookup factory class by type?
j
The actual factory has logic which is needed to build some S3 URLs for various resource types. We use it to hit endpoints both for instances of a class (if we want to retrieve a specific resource) but also to build a prefix in order to list all resources sharing that prefix. So we don't always have an instance to work with and do an
is
check here.