Skip to content

Root functions should allow nullable args#144

Open
adamhathcock wants to merge 1 commit intoDevTeam:masterfrom
adamhathcock:adam/nullable-roots
Open

Root functions should allow nullable args#144
adamhathcock wants to merge 1 commit intoDevTeam:masterfrom
adamhathcock:adam/nullable-roots

Conversation

@adamhathcock
Copy link
Copy Markdown
Contributor

when doing .RootArg I noticed nullable values weren't being respected/propagated. This PR should allow that.

@adamhathcock
Copy link
Copy Markdown
Contributor Author

I can retarget this PR when dev is made again

@NikolayPianikov
Copy link
Copy Markdown
Member

Thanks for the PR.

Could you please describe the concrete scenario where a root argument needs to be passed as null and then injected into the object graph?

I intentionally avoided supporting this so far for a couple of reasons:

  • Pure.DI tries to keep generated object graphs predictable and validated at compile time. Today, when a dependency is injected, the consumer can generally rely on the injected reference not being null; constructors do not need defensive null checks for DI-provided objects.

  • Root arguments behave like normal bindings: they are values that can be injected anywhere in the root object graph. If we allow null for a root argument, the same value may later be injected into a constructor parameter/property that is declared as non-nullable. That weakens the current guarantee and may turn a compile-time wiring concern into a runtime nullability problem.

  • Nullability annotations are not part of the runtime type identity. For dependency resolution, string and string? are still the same type. So allowing nullable root arguments is not just a signature-generation change; it affects how safely Pure.DI can propagate that value through the graph.

For optional data, I would currently expect users to model it explicitly, for example with a wrapper/options type, a domain-specific abstraction, or a default value, depending on the scenario.

I’m open to supporting this if there is a strong use case, but I would like to understand the intended pattern first and make sure it does not break Pure.DI’s expectation that injected dependencies are non-null unless explicitly modeled otherwise.

@adamhathcock
Copy link
Copy Markdown
Contributor Author

Real quick: I'm making a factory class that takes a nullable string and doing something in the factory method.

Depending on the value stuff is done and a new class with some injected dependencies is made.

You're right that dependencies themselves shouldn't be nullable but passed args can be.

I can give more details and stuff on Tuesday. Typing on mobile now. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants