-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KoltinModule cannot be created using createInstance() #291
Comments
Also, version 2.10.10, specified in README, doesn't actually exist. |
@angryziber what is your use case for |
The use case is Dependency Injection, or more specifically, auto-creation of dependencies |
@angryziber I do not think we can remove the second constructor in upcoming versions (it's necessary for ABI compatibility), maybe in 3.+ release. class Github291 {
fun <T : Any> KClass<T>.createInstanceByPrimaryConstructor(): T {
val allArgsInDefConstructorAreOptional = primaryConstructor?.parameters?.all(KParameter::isOptional) ?: false
if (!allArgsInDefConstructorAreOptional) throw IllegalArgumentException("There is no primary constructor with default arguments: $this")
return primaryConstructor?.callBy(emptyMap())
?: throw IllegalArgumentException("There is no primary constructor: $this")
}
@Test
fun createInstanceOfKotlinModule() {
val kotlinModule = KotlinModule::class.createInstanceByPrimaryConstructor()
assertNotNull(kotlinModule)
}
} Will it suit your case? |
Closing for inactivity |
Fails in runtime with:
If I create exactly the same class declaration in my own code (with default parameters), then my class works with
KClass.createInstance()
. It's probably an issue with some non-default compiler options or version of Kotlin compiler that you are using?Tested with 2.10.2 and Kotlin 1.3.61
The text was updated successfully, but these errors were encountered: