I think i am having a problem using resultBuild with enums, is that normal or compile improve need?
@resultBuilder
struct ResultBuilder<T>
{
static func buildBlock(_ block: [T]...) -> [T]
{
block.flatMap { $0 }
}
....
}
enum PathModifier
{
case move(to: CGPoint)
case addLine(to: CGPoint)
case stroke
func modify(_ path: NSBezierPath)
{
switch self {
case .move(let point):
path.move(to: point)
case .addLine(let point):
path.line(to: point)
case .stroke:
path.stroke()
}
}
}
extension NSBezierPath
{
@discardableResult
convenience init(@ResultBuilder<PathModifier> _ modifiers: () -> [PathModifier])
{
self.init()
modifiers().forEach {
$0.modify(self)
}
}
@discardableResult
convenience init(modifiers: [PathModifier])
{
self.init()
modifiers.forEach {
$0.modify(self)
}
}
}
As you see, i hava above code, then if i try to write this, it would not work:
NSBezierPath {
.move(to: .zero)
.addLine(to: CGPoint(1, 1))
.stroke
}
the compile says: Enum case 'addLine' cannot be used as an instance member
But if i write like this, it works:
NSBezierPath {
PathModifier.move(to: .zero)
PathModifier.addLine(to: CGPoint(x: 1, y: 1))
PathModifier.stroke
}
and this works to:
NSBezierPath(modifiers: [
.move(to: .zero),
.addLine(to: CGPoint(x: 1, y: 1)),
.stroke
])
the problem is i like to write code more like DLS and if I have to write PathModifier each time, this would be little be bothering don't you think? It there anyway i can improve this?