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

Non transient value type error #163

Merged
merged 8 commits into from
Dec 23, 2023

Conversation

AlonTalmi
Copy link
Contributor

If you register a value type as singleton or scoped you get a compile error.

The compile error happens because there's a nullable field saved for each registration, which in reference type is implicitly castable to the implementation type, but in value type you have to cast it manually or use the Value property.

The 1st commit is a Unit Test showcasing the problem

valueCallback(codeWriter, w => w.Append($"{cacheLocation}"));
if (serviceCallSite.ImplementationType.IsValueType)
{
valueCallback(codeWriter, w => w.Append($"{cacheLocation}!.Value"));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ! required? There is an if ... == null check right above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, but not really, it's a == default check which still causes the compiler to throw an error, I can change it to == null and remove the !

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== null sounds good.

@pakrym pakrym merged commit f8adaef into pakrym:main Dec 23, 2023
6 checks passed
@pakrym
Copy link
Owner

pakrym commented Dec 23, 2023

Thank you!

@AlonTalmi AlonTalmi deleted the NonTransientValueTypeError branch December 24, 2023 05:33
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