Skip to content

Commit

Permalink
Remove default Never body, refine error messages
Browse files Browse the repository at this point in the history
Resolve #110.
  • Loading branch information
MaxDesiatov committed Jun 22, 2020
1 parent 9f6cd42 commit 5234266
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Sources/Tokamak/Views/AnyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public struct AnyView: View {
}
}
}

public var body: Never {
neverBody("AnyView")
}
}

public func mapAnyView<T, V>(_ anyView: AnyView, transform: (V) -> T) -> T? {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Tokamak/Views/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public struct Button<Label>: View where Label: View {
self.label = label()
self.action = action
}

public var body: Never {
neverBody("Text")
}
}

extension Button where Label == Text {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Tokamak/Views/HStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public struct HStack<Content>: View where Content: View {
self.spacing = spacing
self.content = content()
}

public var body: Never {
neverBody("HStack")
}
}

extension HStack: ParentView {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Tokamak/Views/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public struct Text: View {
public init<S>(_ content: S) where S: StringProtocol {
self.content = String(content)
}

public var body: Never {
neverBody("Text")
}
}

public func textContent(_ text: Text) -> String {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Tokamak/Views/TupleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
//

public struct TupleView<T>: View {
public var body: Never {
neverBody("TupleView")
}

public let value: T

// FIXME: should not be public
Expand Down
4 changes: 4 additions & 0 deletions Sources/Tokamak/Views/VStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public struct VStack<Content>: View where Content: View {
self.spacing = spacing
self.content = content()
}

public var body: Never {
neverBody("VStack")
}
}

extension VStack: ParentView {
Expand Down
13 changes: 8 additions & 5 deletions Sources/Tokamak/Views/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ public protocol View {
}

extension Never: View {
public typealias Body = Never
}

public extension View where Body == Never {
var body: Never { fatalError() }
public var body: Never {
neverBody("Never")
}
}

/// A `View` type that renders with subviews, usually specified in the `Content` type argument
Expand All @@ -40,3 +38,8 @@ protocol GroupView: ParentView {}
public protocol ViewDeferredToRenderer {
var deferredBody: AnyView { get }
}

/// Calls `fatalError` with an explanation that a given `type` is a primitive `View`
public func neverBody(_ type: String) -> Never {
fatalError("\(type) is a primitive `View`, you're not supposed to access its `body`.")
}
8 changes: 8 additions & 0 deletions Sources/Tokamak/Views/ViewBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@

public struct EmptyView: View {
@inlinable public init() {}

public var body: Never {
neverBody("EmptyView")
}
}

// swiftlint:disable:next type_name
public enum _ConditionalContent<TrueBranch, FalseBranch>: View
where TrueBranch: View, FalseBranch: View {
case trueBranch(TrueBranch)
case falseBranch(FalseBranch)

public var body: Never {
neverBody("_ConditionalContent")
}
}

@_functionBuilder public struct ViewBuilder {
Expand Down
4 changes: 4 additions & 0 deletions Sources/TokamakDOM/Views/HTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public struct HTML<Content>: View, AnyHTML where Content: View {
}

var innerHTML: String? { nil }

public var body: Never {
neverBody("HTML")
}
}

extension HTML where Content == EmptyView {
Expand Down

0 comments on commit 5234266

Please sign in to comment.