Skip to content

AutoDeconstruct #375

@distantcam

Description

@distantcam

Ref #323

Thinking about having the ability to deconstruct objects in the constructor, without having to use an initializer.

[AutoConstruct]
public partial class TheClass
{
	[AutoDeconstruct<IOptions<Config>>]
	private readonly Config _config;
}

public static class Deconstructors
{
	[AutoDeconstructor]
	public static TConfig Deconstruct<TConfig>(IOptions<TConfig> options)	
	{
		return options.View;
	}
}

Generated:

public partial class TheClass
{
	public TheClass(IOptions<Config> options)
	{
		_config = Deconstructors.Deconstruct(options);
	}
}
  • Only need to write the deconstructor pattern once, can be used multiple times
  • What about deconstructing multiple values, e.g. tuples?

See also https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/deconstruct

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions