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

Allow parameterless constructors in record structs #52240

Closed
jcouv opened this issue Mar 30, 2021 · 4 comments · Fixed by #55042
Closed

Allow parameterless constructors in record structs #52240

jcouv opened this issue Mar 30, 2021 · 4 comments · Fixed by #55042

Comments

@jcouv
Copy link
Member

jcouv commented Mar 30, 2021

[This issue is referenced in source]

When the "record structs" feature gets merged with the "parameterless struct constructor" feature, we may need to make some adjustments to record structs.

Some scenarios are interesting:

record struct S1(); // is this allowed?
_ = new S2(); // what is executed?
record struct S2(int I)
{
   int Field = M(I);
   static int M(int i) => i;
}
record struct S3
{
  int Field = 3; // is this allowed?
}
record struct S4()
{
  int Field = 3; // is this allowed?
}
@jcouv jcouv added this to the C# 10 milestone Mar 30, 2021
@jcouv jcouv self-assigned this Mar 30, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Mar 30, 2021
@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Mar 30, 2021
@jaredpar jaredpar modified the milestones: C# 10, 17.0 Jul 13, 2021
@SteveDunn
Copy link
Contributor

SteveDunn commented Jul 22, 2021

Hi - is this in .NET 6 preview 6? It'd be great if a user could specify a validation method so that the struct could never be created with an invalid value, e.g.

public record struct Age(int Value)
{
   public Age()
  {
    if(Value < 0) throw new InvalidOperationException("Ages cannot be negative");
  } 
}

@jcouv
Copy link
Member Author

jcouv commented Jul 22, 2021

No, it barely missed .NET 6 preview 7. It'll be in the following preview.

@SteveDunn
Copy link
Contributor

SteveDunn commented Jul 22, 2021

I'm ideally looking for the succinctness of records, the memory traits of value-type primitives, and constraits that mean no invalid instances can be created. https://dunnhq.com/posts/2021/primitive-obsession/

@jaredpar jaredpar added the Bug label Aug 2, 2021
@jcouv
Copy link
Member Author

jcouv commented Aug 2, 2021

Update: it did make it into .NET 6 preview 7 after all (and VS 2022 preview 3).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants