Stroniax
Stroniax
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
I will try to use more common nouns.
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
Oh gotcha.
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
Yes, I'm not sure what you disliked about my example code
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
C#
public record Dto(int Id);
C#
public record Dto(int Id);
@MKP does this help? Based on the example code in OP.
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
Like Span. It's nbd to call it through a sync method.
C#
public async Task DoSomethingAsync() {
var dto = await GetSomeDto();
var my = Build(dto);
await DoSomethingWith(my);
}
private My Build(Dto someDto) => new MyBuilder().WithId(someDto.Id).Build();
C#
public async Task DoSomethingAsync() {
var dto = await GetSomeDto();
var my = Build(dto);
await DoSomethingWith(my);
}
private My Build(Dto someDto) => new MyBuilder().WithId(someDto.Id).Build();
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
Gotcha. Good reason to avoid it then.
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
I mean it's... unsafe, I'm sure there's a reason for that which I simply don't understand of why not to use that method here.
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
Or what if I do Unsafe.AsRef(in builder) inside the create method?
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
I think I will just document and instruct users to create the builder in a separate line from the build chain. Not as great UX but it's what works.
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
👍 that is what I was afraid of, thanks for confirming
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
Wait, I didn't see the static self. That's not at all thread safe, is it?
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
Oh shoot. I'm OK with that if it's safe to do?
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
@MKP New example that I believe is more concise. Does this help?
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
C#
public struct Example {
public int First;
// for simplicity, I have one member. This is source-generated: imagine the example with twenty members and equally many builder extensions
}
public static class ExampleExtensions {
// I do not want to accept a copy of this instance, but a reference to the instance itself
public static Example WithFirstByCopy(this Example e, int first) {
e.First = first;
// I do not want to return a copy of this instance, but the ref instance I would like to receive
return e;
}
public static ref Example WithFirstByRef(ref this Example e, int first) {
e.First = first;
return ref e;
}
// This does not compile so it does not suit my needs
public static Example DoesNotCompile() {
return new Example().WithFirstByRef(1);
}
// This copies 3? times so it does not suit my needs
public static Example CopiesBuilder() {
return new Example().WithFirstByCopy(1);
}
}
C#
public struct Example {
public int First;
// for simplicity, I have one member. This is source-generated: imagine the example with twenty members and equally many builder extensions
}
public static class ExampleExtensions {
// I do not want to accept a copy of this instance, but a reference to the instance itself
public static Example WithFirstByCopy(this Example e, int first) {
e.First = first;
// I do not want to return a copy of this instance, but the ref instance I would like to receive
return e;
}
public static ref Example WithFirstByRef(ref this Example e, int first) {
e.First = first;
return ref e;
}
// This does not compile so it does not suit my needs
public static Example DoesNotCompile() {
return new Example().WithFirstByRef(1);
}
// This copies 3? times so it does not suit my needs
public static Example CopiesBuilder() {
return new Example().WithFirstByCopy(1);
}
}
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
If the code ran, I wouldn't be needing help here 🙂 Let me see if I can come up with a more simple example
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
The point is that this won't build, I'm trying to avoid having to assign to a field/variable first while mutating and returning the same struct instance, that's my primary goal. I've added a My definition if necessary, but the point is that the builder just builds into it.
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
No description
58 replies
CC#
Created by Stroniax on 1/27/2024 in #help
Builder Pattern as Ref Struct
What's weird is that I don't need to assign intermediate stages.
58 replies