spand
10/07/2019, 6:29 AMoperator val invoke
property ? The error message seems to suggest its possible but I cant find a way
Modifier 'operator' is not applicable to 'member property without backing field or delegate'
class Foo {
companion object {
operator val invoke = {}
}
}
Sebastian Sellmair [JB]
10/07/2019, 6:29 AMSebastian Sellmair [JB]
10/07/2019, 6:30 AMspand
10/07/2019, 6:36 AMinterface Foo {
var prop1: String
var prop2: String
companion object {
operator fun invoke(prop1: String, prop2: String) = object : Foo {
override var prop1 = prop1
override var prop2 = prop2
}
}
}
So maybe this would be a shorter way to express the same:
interface Foo2 {
var prop1: String
var prop2: String
class Helper(override var prop1: String, override var prop2: String) : Foo2
companion object {
operator fun val invoke = ::Helper::invoke
}
}
kralli
10/07/2019, 6:48 AMinterface Foo2 {
var prop1: String
var prop2: String
class Helper(override var prop1: String, override var prop2: String) : Foo2
companion object {
operator fun invoke(prop1: String, prop2: String) = Helper(prop1, prop2)
}
}
spand
10/07/2019, 6:51 AMprop1
kralli
10/07/2019, 7:01 AMinvoke
operator function is the only way to call Foo2
directly.spand
10/07/2019, 7:11 AMoperator val
.Ruckus
10/07/2019, 2:40 PMoperator
can't be used there, and to make a nice message, it generates a string representation of your use, which in this case happens to be on a "member property without backing field or delegate".