10000 Added support for View optionals, fixing @ViewBuilder conditionals. by Samuel-IH · Pull Request #58 · SwiftWebUI/SwiftWebUI · GitHub
[go: up one dir, main page]

Skip to content

Added support for View optionals, fixing @ViewBuilder conditionals. #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/SwiftWebUI/Views/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ public protocol View {
var body : Self.Body { get }

}

//support for optional Views, mainly for @ViewBuilder convenience
extension Optional : View where Wrapped : View {
public var body: EmptyView {
EmptyView()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better late than never, but that seems wrong. The Optional View should be the wrapped if set, and EmptyView only in the .none case.

}
}
4 changes: 1 addition & 3 deletions Sources/SwiftWebUI/Views/ViewBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

public extension ViewBuilder {

static func buildIf<V: View>(_ content: V) -> V { return content }
//static func buildIf<V: View>(_ content: V?) -> V? { return content }
// This one still doesn't work!
static func buildIf<V: View>(_ content: V?) -> V? { return content }

static func buildEither<T: View, F: View>(first: T)
-> ConditionalContent<T, F>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ extension ComponentTypeInfo {
return nil
}
guard structInfo.kind == .struct else {
if structInfo.kind == .optional {
self = .static
return
}
print("Only structs allowed as View:", viewType)
assertionFailure("currently only supporting structs for Views")
return nil
Expand Down
0