I have a function that loads an image, and if it can’t load the image I want to load a placeholder. It doesn’t have any internal state, so it would make sense to make it a top level function. However, I think I need a class loader to load the image from my resources directory. Since it would be the same placeholder image every time, I think it should be static and only loaded once. Here is how I am currently solving this problem.
class ImageLoader {
companion object {
@JvmField val placeholderImage = Image(ImageLoader::class.java.getResource("/placeholder.jpg").toURI().toString())
}
fun load(filename: String): Image { ... }
}
Using a companion object and jvm annotation seems wrong to me. Is there a better way I can do this?