https://kotlinlang.org logo
#codereview
Title
# codereview
j

Jonathan Mew

10/11/2021, 4:29 PM
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

Jakob

10/11/2021, 8:10 PM
Personally i just keep unchecked cast for these cases. Idk if the typechecker is smart enough to do this 100% type-safe
👍 1
t

Tomasz Krakowiak

10/11/2021, 8:32 PM
Why lookup factory class by type?
j

Jonathan Mew

10/13/2021, 8:30 AM
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.
10 Views