InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
9.12k stars 721 forks source link

Function as parameter #811

Closed Janekxyz closed 4 years ago

Janekxyz commented 4 years ago

Is is possible to inject function as a parameter? I'm trying to inject function to class but it's return CastException: Caused by: java.lang.ClassCastException: MyCustomFragment$adapter$2$1 cannot be cast to kotlin.jvm.functions.Function1

koin_version = '2.1.5'

factory { (func: () -> Unit) ->  Adapter(func) }

class Adapter(private val func: () -> Unit)

private val adapter: Adapter by inject { parametersOf( {} ) }
arttttt commented 4 years ago

Hi, I checked this case and there were no problems.

class TestClass(val callback: () -> Unit) {}

factory { (callback: () -> Unit) -> TestClass(callback) }

private val testClass by scope.inject<TestClass> {
    parametersOf({
        Timber.e("TEST")
    })
}

testClass.callback()

2020-05-23 12:25:11.977 3347-3347/com.arttttt.koinsample E/FragmentB$testClass: TEST