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

Generate const fields for applicable types #131

Merged

Conversation

NoahStolk
Copy link
Contributor

@NoahStolk NoahStolk commented May 30, 2024

Closes #125

This generates const fields for applicable types, for example:

public partial class CustomerType : 
    global::System.IEquatable<CustomerType>, 
    global::System.IComparable, 
    global::System.IComparable<CustomerType> 
{
    // const fields...
    public const System.Int32 NormalValue = 0;
    public const System.Int32 GoldValue = 1;
    public const System.Int32 DiamondValue = 2;

This allows us to write:

public partial class CustomerType
{
    public string GetIcon()
    {
        return Value switch
        {
            NormalValue => "🟢",
            GoldValue => "🟡",
            DiamondValue => "🔵",
            _ => throw new UnreachableException(),
        };
    }
}

instead of:

public partial class CustomerType
{
    public string GetIcon()
    {
        if (this == Normal)
            return "🟢";
    
        if (this == Gold)
            return "🟡";
    
        if (this == Diamond)
            return "🔵";

        throw new UnreachableException();
    }
}

The new generator method ignores types that are not constants, such as:

public record class Planet(string Colour, int CircumferenceInMiles)

It would probably be nice to have a test that explicitly validates that these const fields are only generated for primitives, strings, and decimals.

@NoahStolk NoahStolk force-pushed the generate-const-values-if-possible branch from 408fc50 to e7f6a03 Compare May 31, 2024 09:57
@SteveDunn
Copy link
Owner

LGTM! Thanks for the contribution - much appreciated!

@SteveDunn SteveDunn marked this pull request as ready for review June 3, 2024 07:02
@SteveDunn SteveDunn merged commit ae5f1be into SteveDunn:main Jun 3, 2024
2 of 5 checks passed
@NoahStolk NoahStolk deleted the generate-const-values-if-possible branch June 3, 2024 07:30
@arteny
Copy link

arteny commented Jul 5, 2024

it is not working for string underlaying type, getting error:

Severity	Code	Description	Project	File	Line	Suppression State
Error (active)	CS9135	A constant value of type 'ColorStyle' is expected

@NoahStolk
Copy link
Contributor Author

it is not working for string underlaying type, getting error:

Severity	Code	Description	Project	File	Line	Suppression State
Error (active)	CS9135	A constant value of type 'ColorStyle' is expected

@arteny I've tested this extensively with strings and I'm currently using it in my code. It compiles fine. Are you sure you're not using the members in your switch expressions instead of the new const fields? You can only use the const fields (the ones ending in "Value") in switch expressions.

@arteny
Copy link

arteny commented Jul 6, 2024

it is not working for string underlaying type, getting error:

Severity	Code	Description	Project	File	Line	Suppression State
Error (active)	CS9135	A constant value of type 'ColorStyle' is expected

@arteny I've tested this extensively with strings and I'm currently using it in my code. It compiles fine. Are you sure you're not using the members in your switch expressions instead of the new const fields? You can only use the const fields (the ones ending in "Value") in switch expressions.

Ahh, understood now, we need to use other member with Value ending. Looks like it is workint well. Thank you.

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.

Feature Request: Const Value Generations to get around "A constant value is expected"
3 participants