Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What this PR does:
This should also provide a bit more type safety. This is similar to how Anko works with
{ ctx -> View(ctx) }
constructor.newInstance(context)
could have failed.I haven't made any benchmarks yet, but I think this should provide some performance improvements versus
viewClass.getConstructor(Context.class).newInstance(c);
Besides this, in Kotlin you get a rather nice syntax with
v(MyCustomView(it))
.The only issue with this approach, that I could see, is that these functions should be cached, otherwise Anvil will reinstantiate the view on every render. Which is a pretty big deal.
Fortunately, Retrolambda and Kotlin caches lambdas, so this should work fine:
v((ctx) -> { new MyCustomView(ctx) })
and this
v({ ctx -> MyCustomView(ctx) })
and this
v({ MyCustomView(it) })
Kotlin (and maybe Retrolambda, didn't check), on the other hand, does not cache method references.
So while this is a pretty nice syntax:
v(::MyCustomView) { /* do something */ }
the problem is that it would instantiate the view on every render.
Any thoughts?