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

Suggestion - DefaultValue #599

Open
Aragas opened this issue May 21, 2024 · 2 comments
Open

Suggestion - DefaultValue #599

Aragas opened this issue May 21, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Aragas
Copy link

Aragas commented May 21, 2024

Describe the feature

In my real life scenarios I have a concept of DefaultValue - it's mostly used in my custom TryParse scenarios, when the parsing failed, but the value still needs to be valid an initialized with something meaningful.
A possible implementation would be to have a static property DefaultValue, this way we also should be able to use with Static Abstract Members if needed for custom solutions.
The only note I have, since not every type has a default value, it would make sense to have a separate interface IVogenHasDefaultValue<>

[ValueObject<byte>]
[Instance("None", "0")]
public readonly partial record struct TenantId
{
    // Either manually or via Instance
    //public static readonly byte None = From(0);

    public static byte DefaultValue => None;
    // ...
    // Generated Code
    public static global::System.Boolean TryParse(global::System.ReadOnlySpan<byte> utf8Text, 
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
 out TenantId result) {
        if(System.Byte.TryParse(utf8Text, out var __v)) {
            
            
            result = new TenantId(__v);
            return true;
        }

        result = DefaultValue; // instead of `= default`;
        return false;
    }
}
@Aragas Aragas added the enhancement New feature or request label May 21, 2024
@SteveDunn
Copy link
Owner

Thanks for the feedback and apologies for the very slow response!

It could check for the special DefaultValue field (or property).

As a workaround, you could add your own TryParse and delegate to the generated one that one has a format provider. Something like:

    public static bool TryParse(ReadOnlySpan<byte> utf8Text, out TenantId result)
    {
        bool parsed = TryParse(utf8Text, CultureInfo.InvariantCulture,  out result);
        result = parsed ? result : None;

        return parsed;
    }

@Aragas
Copy link
Author

Aragas commented Sep 25, 2024

Yea, I believe something like this is already used as a workaround by me, but it will be appreciated if there will be a default value behavior. Could be especially interesting for strings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants