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

Support .NET 8 Collection Expressions [] #159

Open
Patonymous opened this issue Jul 29, 2024 · 5 comments
Open

Support .NET 8 Collection Expressions [] #159

Patonymous opened this issue Jul 29, 2024 · 5 comments

Comments

@Patonymous
Copy link

File:

public class DTO
{
    public List<Result> Results { get; set; } = [];
}

Output:

Cannot compile contracts. There were errors during project compilation: Contracts compilation failed. Errors:
[Error] Invalid expression term '[' ...
@jakubfijalkowski
Copy link
Member

Unfortunately, because of how MSBuild works, supporting .NET higher than 6 in contracts is mostly impossible in the current architecture.

Also, initializing properties directly in DTOs is considered an anti-pattern, as this is a representation of the mythical logic.

@mishioo
Copy link
Contributor

mishioo commented Jul 30, 2024

Oh, right. I suggested posting an issue because I forgot that the problem is with MSBuild rather than just a missing portion of the reflection.

Also, initializing properties directly in DTOs is considered an anti-pattern, as this is a representation of the mythical logic.

I somewhat disagree with this, though. For me, it's more about providing a saner default value than null for non-nullable reference types that have a natural default (so, mostly for collections, using an empty collection as the default). I think it's better than an unexpected null, at least until we can't use required...

@jakubfijalkowski
Copy link
Member

I think it's better than an unexpected null, at least until we can't use required...

Exactly - with .NET 8, required should be used, but not default initialization. :P

@mishioo
Copy link
Contributor

mishioo commented Jul 30, 2024

I think it's better than an unexpected null, at least until we can't use required...

Exactly - with .NET 8, required should be used, but not default initialization. :P

The point is that we can't do that, required is not available in projects targeting .NET 6, so we can't use it in contracts. That is unless without using something line PolySharp, which I doubt we'd like to incorporate.

@jakubfijalkowski
Copy link
Member

jakubfijalkowski commented Jul 30, 2024

Yes, I know - I'm just saying that provided that we can use .NET 8, then required should be used and not the = [] default initialization (nor = new List<Result>() in older versions).

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

No branches or pull requests

3 participants