So, this is the context: I have a set of custom shapes, say for instance ShapeA, ShapeB, ShapeC, ShapeD, ShapeE, ShapeF
, all of them conforming to a user defined protocol like this:
protocol HasNormalizedAABB: Shape {
func normalizedAABB() -> CGRect
}
It is very clear that HasNormalizedAABB
also requires conformance to Shape
, that in turn requires conformance to View
, so HasNormalizedAABB
concretions are also View
s.
Then somewhere else in the code there is an enum
that determines which of those shapes is visible at the moment, say for the sake of this example:
enum VisibleShape {
case A
case B
case C
case D
case E
case F
}
Now I need a way to map each possible state of this enum
to its corresponding HasNormalizedAABB
shape and organically apply a styling to whatever the rendered shape is. Keep in mind that I could need to do the same in 500 different views with 500 different styles applied to it. This makes it absolutely critical to create a way to parametrize the style instead of creating a different mapping for each style.
This is what I tried:
@ViewBuilder
static func EnumToShape(
enumState: VisibleShape,
transform: @escaping (any HasNormalizedAABB) -> some View
) -> some View {
switch enumState {
case .A:
transform(ShapeA())
case .B:
transform(ShapeB())
case .C:
transform(ShapeC())
case .D:
transform(ShapeD())
case .E:
transform(ShapeE())
case .F:
transform(ShapeF())
}
}
Which is supposed to be used as follows:
EnumToShape(enumState: myStateVariableOfTypeVisibleShape) {
$0
.stroke(.green, lineWidth: 2)
.shadow(color: .green, radius: 4)
}
Which causes Type 'any View' cannot conform to 'View'