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

Source generator for lazy properties and methods #1033

Open
Rekkonnect opened this issue Dec 24, 2024 · 0 comments
Open

Source generator for lazy properties and methods #1033

Rekkonnect opened this issue Dec 24, 2024 · 0 comments
Labels
feature request 📬 A request for new changes to improve functionality

Comments

@Rekkonnect
Copy link

Overview

Provide a source generator that will make it quicker to define a lazily-instantiable property value. The underlying mechanism may use the provided System.Lazy<T> type. The generator may also support the partial properties feature introduced in C# 13.

This will be an alternative option to Lombok.NET's Lazy generator. The reason being that Lombok forces an irregular structure, instantiating a class instance to hold the value and refer to its instance holding the globally initialized lazy value. It's also less flexible, not conveniently supporting per-instance lazy instances out of the box.

API breakdown

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, Inheritable = false, AllowMultiple = false)]
public class LazyAttribute(string methodName) : Attribute
{
    public string MethodName { get; } = methodName;
}

Usage example

public class C
{
    [Lazy(nameof(CalculateHeavyName))]
    public partial string HeavyName { get; }

    // assume heavy calculation here
    private string CalculateHeavyName() => string.Empty;
}

Breaking change?

No

Alternatives

As listed above, Lombok.NET offers a source generator for generating lazily-calculated instances. To avoid conflicts with Lombok's Lazy, we could provide an alternative name to the attribute.

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

@Rekkonnect Rekkonnect added the feature request 📬 A request for new changes to improve functionality label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request 📬 A request for new changes to improve functionality
Projects
None yet
Development

No branches or pull requests

2 participants
@Rekkonnect and others