I would like to use a default value for a function parameter in a class implementing an interface, like this:
interface FileStoreService {
fun storeFile(path: String, payload: InputStream, type: MediaType, replace: Boolean = true)
}
class LocalFileStoreService : FileStoreService {
override fun storeFile(path: String, payload: InputStream, type: MediaType, replace: Boolean /* ?? */) {
// ....
}
}
Now here is what compiles and here is what doesn't compile:
KO: An overriding function is not allowed to specify default values for its parameters
class LocalFileStoreService : FileStoreService {
override fun storeFile(path: String, payload: InputStream, type: MediaType, replace: Boolean = true) {
// ....
}
}
KO: Class 'LocalFileStoreService' is not abstract and does not implement abstract member public abstract fun storeFile(path: String, payload: InputStream, type: MediaType): Unit defined in fqn...FileStoreService
class LocalFileStoreService : FileStoreService {
override fun storeFile(path: String, payload: InputStream, type: MediaType, replace: Boolean) {
// ....
}
}
OK:
class LocalFileStoreService : FileStoreService {
override fun storeFile(path: String, payload: InputStream, type: MediaType) {
storeFile(path, payload, type, true)
}
override fun storeFile(path: String, payload: InputStream, type: MediaType, replace: Boolean) {
// ....
}
}
Is this the expected behaviour? Is there a better way to manage default parameter values in interfaces?