Skip to content
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

Kotlin optimizations #305

Closed
markchristopherng opened this issue Oct 23, 2018 · 5 comments
Closed

Kotlin optimizations #305

markchristopherng opened this issue Oct 23, 2018 · 5 comments
Milestone

Comments

@markchristopherng
Copy link

Hi,

Will you guys be adding any Kotlin extensions in the future?

@PaulWoitaschek
Copy link

There are some things that would be nice for kotlins.

  • The bind(x).to(y) syntax is bad as it conflicts with kotlins to extension function.
    It suggest adding a bindToClass method alias because that's more convenient on kotlin
  • All that bind-to-withProvider-singletonInInstance could be abstracted into a convenient DSL.
  • We could have an extension function on with the scope as the receiver for simpler injection without that Toothpick.inject boilerplate:
fun Scope.inject(target: Any) {
  Toothpick.inject(target, this)
}

@chenthorne
Copy link
Collaborator

chenthorne commented Feb 6, 2019

We've started using a set of custom written extensions that we'd like to make public and incorporate with the toothpick library as a new module. There's a sample project at https://github.com/Leafly-com/toothpick-extensions-sample that has the extension code as well as examples on how it could be used.

  • Extensions to allow use of Kotlin class objects (no more .java)
  • Simple set of infix functions with a DSL for declaring Modules
  • Kotlin Delegate based lazy injection that allows private val dep: Dep by inject()
  • Android specific extensions that search for a scope or assume a default one for activities and fragments
  • Inject with late created scope but still specify as private class member

Would greatly appreciate feedback.

Quick Preview:

// Inject with Kotlin Class
scope.getInstance(KotlinClass::class)

// In a Module, bind with Kotlin Class
bind(KotlinClass::class)...

// Inject dependency safely without specifying type via reified types
val dependency: Dependency = inject(scope)

// Lazy inject dependency via delegate
val dependency: Dependency by injectLazy(scope)

// Will inject with Toothpick.openScope(activityRef.application, activityRef)
class MyActivity : Activity() {
    private val dependency1: Dependency by inject()
}

// Will inject with lazy provided scope
class MyActivity : Activity() {
    private lateinit var lateScope: Scope
    private val dependency1: Dependency by inject { scope = lateScope }
    ...
}

// Binding functions
bind(Dep::class) providedBy DepProvider()
bind(Dep::class) singletonPovidedBy DepProvider::class
bind(Dep::class) named "DepName" providedBy DepProvider()

// DSL
module {
   Picasso::class providedBy PicassoProvider::class 
}

@dimsuz
Copy link

dimsuz commented Feb 6, 2019

We have a shorter getInstance(), not based on a KClass like yours though, works great for us too:

inline fun <reified T> Scope.instance(tag: String? = null): T = this.getInstance(T::class.java, tag)
val c = scope.instance<MyClass>()
val c1: MyClass = scope.instance()

@chenthorne
Copy link
Collaborator

@dimsuz Nice! I'll update to to use that pattern.

@stephanenicolas
Copy link
Owner

This is getting closed as it has been released in version 3.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants