question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: make assisted arguments explicit

See original GitHub issue

kotlin-inject’s way of injecting dependencies with assisted arguments is really clever but I’ve been thinking it’s very implicit and hidden for readers. Thoughts on making it explicit in some manner? Using dagger-like @Assisted annotation is one option, but I’m not sure if you’d want that. It’ll also remove the restriction of keeping assisted arguments at the end of a constructor, which is another hidden restriction.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

3reactions
evantcommented, Aug 5, 2022

Ok here’s my proposal:

Introduce an @Assisted annotation to make assisted arguments explicit

problem

It can be not obvious which arguments are assisted or which are expected to be provided by the graph. This can make it confusing on why you are unable to inject a certain type. While there is extra flexibility with this on choosing which args you want to inject or not at the usage site, this feature isn’t often used.

solution

All assisted arguments must be annotated with an @Assisted annotation. This will limit the usage site to providing exactly those parameters in the same order they are declared. For example with the declaration:

@Inject class AssistedClass(@Assisted arg1: One , arg2: Two, @Assisted arg3: Three)

the only allowed way to inject that class is

@Inject Usage(createAssistedClass: (One, Three) -> AssistedClass)

these will fail to compile with an error about missing or mismatched args

@Inject Usage(createAssistedClass: (One) -> AssistedClass) // missing arg
@Inject Usage(createAssistedClass: (Three, One) -> AssistedClass) // mismatched args
@Inject Usage(createAssistedClass: AssistedClass) // missing args

migration

When this feature is implemented the behavior will be selected based on if there’s any args annotated with @Assisted. If so the new strategy will be used, otherwise a warning will be printed to suggest annotating the arg. This warning will then become an error in a future release.

2reactions
evantcommented, Aug 5, 2022

You mean something like this? 😉 (in reality I’d probably go for something a bit different since the lists aren’t expected to be super long but yea)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proposal: A built-in Go error check function,
The ideas discussed in the draft design centered around a new unary operator check which simplified explicit checking of an error value returned ......
Read more >
The Explicit Syntax of Implicit Arguments - jstor
The proposal I make is very simple. Both types of implicit arguments consist of a (possibly partial) set of 4>-features, a c|>-set. SIAs...
Read more >
Proposal: Auto-propagated function parameters #2327 - GitHub
The purpose of this proposal is to provide an ergonomic yet explicit way to get access to a set of variables deep in...
Read more >
Why explicit self has to stay - Neopythonic
Bruce Eckel has blogged about a proposal to remove 'self' from the formal parameter list of methods. I'm going to explain why this...
Read more >
A proposal for extending the meaning of the 'explicit' keyword
This approach is supposed to have advantages over using templates to constrain the arguments, such as not requiring template instantiations, and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found