Hey there!
I am currently struggling to find a way to create a result builder for the following.
Lets say I have a type FooBar that consists of a string and an int like so:
struct FooBar {
var foo: String
var bar: Int
}
And I'd like to create a result builder to construct this type. What I'd like to accomplish is the following DSL:
This should then construct either FooBar(foo: "Foo", bar: 42) or FooBar(foo: "Bar", bar: 42) depending on the condition. Is there a way to do that or can result builders simply not support that type of DSL?
Ah, the generics were the missing part. I always tried to do something like buildEither(first component: String) -> String and the compilers diagnostic wasn't very helpful. Thanks a ton!
And the compilers diagnostic was Cannot convert value of type 'FooBar' to expected argument type 'String' as well as Missing argument for parameter #2 in call.
I thought I have to implement the buildExpression function whereas I would have to implement the same function just as buildBlock and now that I know what was the missing part, it totally makes sense that the compiler was missing the second parameter because it only saw the buildBlock function that takes two arguments.
I think the problem for me was that its not obvious what code the compiler is actually synthesizing and of course that I simply was not knowing that the buildExpression function is the wrong one to implement.