ra1028 / Carbon

🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.
https://ra1028.github.io/Carbon
Apache License 2.0
1.33k stars 97 forks source link

Mismatched header types #52

Closed wousser closed 5 years ago

wousser commented 5 years ago

Checklist

Expected Behavior

In RC2 it was possible two show a different heading based on a flag

Current Behavior

In RC4 this behavior is not possible anymore. Resulting in this error: Result values in '? :' expression have mismatching types 'TodayEmpty' and 'TodayHeader'

Detailed Description (Include Screenshots)

Section(
                id: "contactToday",
                header: state.today.isEmpty
                    ? TodayEmpty() : TodayHeader(),
                cells: { }
)

Both are Component: struct TodayEmpty : Component struct TodayHeader : Component

Environment

Is this possible in RC4 and onwards?

wousser commented 5 years ago

In the new Todo example, I see

if state.todos.isEmpty {
                Section(id: ID.task, header: TodoEmpty())
            }
            else {
                Section(
                    id: ID.task,
                    header: Header("TASKS (\(state.todos.count))"),
                    cells: { }
)

Is the correct way from now on to specify a Section for each header?

ra1028 commented 5 years ago

@wousser Generic parameters require the same type for both components at compile time. This follows the specification of SwiftUI. Until rc2, ViewNode has played the role of type-erasure, but that isn't the case now. You can use AnyComponent instead.

Section(
    id: "contactToday",
    header: state.today.isEmpty
        ? AnyComponent(TodayEmpty()) 
        : AnyComponent(TodayHeader()),
    cells: { ... }
)