Why extensions method call is ambiguous?
Tag : chash , By : markku
Date : March 29 2020, 07:55 AM
|
Kotlin with Android: Base classes and Kotlin Android Extensions
Date : March 29 2020, 07:55 AM
I hope this helps you . Kotlin Android Extensions generates an extension function for each element on a given layout. Since extension methods exist outside the inheritance model, there is no way to define a common protocol like abstract val toolbar:Toolbar on the parent. However, under the hood the extension methods only execute findById, if the given ID exists on a layout it will fetch the element. This means that if you maintain the same ids for your common elements inside your layouts (i.e.: all toolbars with @id/toolbar), you can create a dummy layout with your common elements and their respective IDs. This layout will work as a sort of interface, allowing you to do import kotlinx.android.synthetic.main.base_activity_dummy.* and thus generating the extension methods you want.
|
Ambiguous functions in multiple protocol extensions?
Tag : swift , By : John Phipps
Date : March 29 2020, 07:55 AM
Hope that helps It's simple to achieve by turning the protocol into a generic (see below), or by creating a type eraser for these protocols, but this very strongly suggests that you have a design problem and you should redesign your classes and/or extensions. A collision like this suggests strongly that MyStruct is doing too many things itself because it's being pulled in multiple directions by MyProtocol1 and MyProtocol2. There should likely be two objects here instead. (Composition rather than inheritance.) class MyStruct: MyProtocol1, MyProtocol2 {
let service = PostService()
func prot1Load<T: MyProtocol1>(t: T) {
t.didLoad()
}
func prot2Load<T: MyProtocol2>(t: T) {
t.didLoad()
}
init() {
prot1Load(self)
prot2Load(self)
}
}
protocol LoadProviding {
func load()
}
struct MyLoader1: LoadProviding {
func load() {
print("MyLoader1.didLoad()")
}
}
struct MyLoader2: LoadProviding {
func load() {
print("MyLoader2.didLoad()")
}
}
protocol Loader {
var loaders: [LoadProviding] { get }
}
extension Loader {
func loadAll() {
for loader in loaders {
loader.load()
}
}
}
class MyStruct: Loader {
let service = PostService()
let loaders: [LoadProviding] = [MyLoader1(), MyLoader2()]
init() {
loadAll()
}
}
typealias LoadProviding = () -> Void
func myLoader1() {
print("MyLoader1.didLoad()")
}
func myLoader2() {
print("MyLoader2.didLoad()")
}
protocol Loader {
var loaders: [LoadProviding] { get }
}
extension Loader {
func loadAll() {
for loader in loaders {
loader()
}
}
}
class MyStruct: Loader {
let service = PostService()
let loaders: [LoadProviding] = [myLoader1, myLoader2]
init() {
loadAll()
}
}
|
How to differentiate between a bound callable member reference and a function of the same type in kotlin?
Tag : java , By : Senthil
Date : March 29 2020, 07:55 AM
I wish did fix the issue. One way to distinguish a lambda from a function reference within the type system is using the reflection interface KFunction , for example:fun <T> f(ref: T) where T : () -> Unit, T : KFunction<Unit> { /* ... */ }
fun g() { println("hello") }
f(::g) // OK
f { println("hello") } // Error: type parameter bound is not satisfied
|
kotlin-android-extensions stop working if lifecycle-extensions is included in gradle
Date : March 29 2020, 07:55 AM
To fix this issue It seems like the issue was that since the navigation dependencies must be (for now) in the android.arch distribution adding androidx dependencies messed with the project. The problem have been solved by replacing the previous conflicting dependency to: implementation("android.arch.lifecycle:extensions:1.1.1")
|