If I have an interface as ```interface StringProv...
# announcements
p
If I have an interface as
Copy code
interface StringProvider {
  operator fun get(key: String): String
  fun getString(key: String): String
  ....
}
And I have a class that contains the key but can not add this
StringProvider
dependency how can I add this block to the constructor so I can get the key?
Copy code
class Foo {
  ....
  var age: String
  var weigh: String
  fun getExample(provider: (String) -> String) : Foo { 
    age = provider["ageprovider"]
    weigh = provider["weighprovider"]
  }
}
So in my class that I have a object of
StringProvider
I send just the
StringProvider
r
maybe you mean smth like
Copy code
fun getExample(getString: (String) -> String) : Foo { 
    age = getString("ageprovider")
    weigh = getString("weighprovider")
  }
and then use it like
Copy code
foo.getExample { stringProvider[it] }
eg, pass a lambda that internally uses provider, but externally it's just
(String) -> String
function if you can change Provider, might be good to let it implement
(String) -> String
too, so you could just do
foo.getExample(provider)
p
I guess yes, but my constructor already contains a lambda, something like this :
Copy code
class Foo (val id: String, getString :(String)->String,onClick: (View) -> Unit)
How do I call these lambdas?
r
you can just simply do
getString(str)
note that currently you can call them only in constructor - as you don't actually store those lambdas
p
I could do this val foo = Foo(id, {provider[it]},{click.invoke()}
is it ok?
Didn't get what you said in
if you can change Provider, might be good to let it implement 
(String) -> String
 too, so you could just do 
foo.getExample(provider)
r
you could do smth like this:
Copy code
interface StringProvider: (String) -> String {
    
    operator fun get(key: String): String
    fun getString(key: String): String

    override operator fun invoke(key: String): String = get(key)
}
now every StringProvider will be also a lambda of
(String) -> String
so you could write
Foo(id, provider, {click.invoke()})
instead of what you wrote (and yes it looks ok, as far as I can see) ofcourse do this implementation only if it makes sense in your domain
(note that I made it extend
(String) -> String
- as functions are proper types (or interfaces? I think interfaces) in Kotlin, you can use them as super types - the invoke operator is overriding the one defined in
(String) -> String
)
p
Didn't get last part
r
basically, with this definition, this works:
Copy code
fun hasProvider(provider: StringProvider) {
    cantHaveProvider(provider)
}

fun cantHaveProvider(getString: (String) -> String) {
    getString("test")
}