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

Null handling. #7

Open
PolarbearDK opened this issue Mar 1, 2018 · 1 comment
Open

Null handling. #7

PolarbearDK opened this issue Mar 1, 2018 · 1 comment

Comments

@PolarbearDK
Copy link

This is a really cool project.

If the result of the body of the property is null, then the body of the property is called each time.

This could be fixed using a slightly different implementation:

// begin - fields added by the post-compile step
private static Lazy<Settings> _settings = new Lazy<Settings>(() => __Settings);
public static Settings Settings { get { return _settings.Value; } }
// end

// Property is made private and name changed somehow
private static Settings __Settings
{
	get
	{
		using (var fs = File.Open("settings.xml", FileMode.Open))
		{
			var serializer = new XmlSerializer(typeof(Settings));
			return (Settings)serializer.Deserialize(fs);
		}
	}
}
@bcuff
Copy link
Owner

bcuff commented Mar 10, 2018

Ah yes. This is a problem for reference types. For value types i wrap the value in a container class I think (it's been a while since I wrote this). I could do the same for reference types but Lazy doesn't have the behavior I want. I want it to:

  1. Guarantee that the function is called exactly once.
  2. Retry should an exception be thrown (Lazy will save and rethrow the exception if one occurs on the first time.)

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

2 participants