leancodepl / contractsgenerator

Other
7 stars 2 forks source link

Support .NET 8 Collection Expressions `[]` #159

Open Patonymous opened 1 month ago

Patonymous commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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).