Returning redacted(reason:) or unredacted() from method causes mismatching types error

The error I am getting is: Result values in '? :' expression have mismatching types 'some View' (result of 'Self.redacted(reason:)') and 'some View' (result of 'Self.unredacted()')
But aren't some View and some View the same thing.

This method is part of a view:
Code Block Swift
func redacted(when active: Bool) -> some View {
active ? redacted(reason: .placeholder) : unredacted()
}

and then used on the view in ContentView like this:
Code Block Swift
TestView()
.redacted(when: isActive)

Even if I use an if statement this error still occurs: Function declares an opaque return type, but the return statements in its body do not have matching underlying types

How can I return either of two modifiers that both return some View from a method that returns some View?
Answered by OOPer in 639972022

But aren't some View and some View the same thing.

NO. some View means some specific type conforming to View. some View here and some View there may or may not be the same type.

How can I return either of two modifiers that both return some View from a method that returns some View?

One way is using AnyView.
Code Block
func redacted(when active: Bool) -> some View {
return active ? AnyView(redacted(reason: .placeholder)) : AnyView(unredacted())
}



Accepted Answer

But aren't some View and some View the same thing.

NO. some View means some specific type conforming to View. some View here and some View there may or may not be the same type.

How can I return either of two modifiers that both return some View from a method that returns some View?

One way is using AnyView.
Code Block
func redacted(when active: Bool) -> some View {
return active ? AnyView(redacted(reason: .placeholder)) : AnyView(unredacted())
}



Returning redacted(reason:) or unredacted() from method causes mismatching types error
 
 
Q