Skip to content
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

Forbid class types for ItemContent and HeaderFooterContent #243

Merged
merged 1 commit into from
Dec 17, 2020
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Added

- [Assert that `ItemContent` and `HeaderFooterContent` are value types](https://github.com/kyleve/Listable/pull/243). This is assumed by the framework and should be validated.

### Removed

### Changed
Expand Down
2 changes: 2 additions & 0 deletions ListableUI/Sources/HeaderFooter/HeaderFooter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public struct HeaderFooter<Content:HeaderFooterContent> : AnyHeaderFooter
layout : HeaderFooterLayout = HeaderFooterLayout(),
onTap : OnTap? = nil
) {
assertIsValueType(Content.self)

self.content = content

self.sizing = sizing
Expand Down
30 changes: 30 additions & 0 deletions ListableUI/Sources/Internal/Validations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Validations.swift
// ListableUI
//
// Created by Kyle Van Essen on 12/15/20.
//

import Foundation


/// Validates that the provided object is not a class type.
func assertIsValueType<Value>(_ valueType : Value.Type) {

#if !DEBUG
return
#endif

precondition(
valueType is AnyClass == false,
{
let typeName = String(describing: Value.self)

return """
`\(typeName)` must be a value type to work properly with Listable and value semantics. Instead, it was a class.

Please convert your `\(typeName)` class from a `class` to a `struct` type.
"""
}()
)
}
2 changes: 2 additions & 0 deletions ListableUI/Sources/Item/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public struct Item<Content:ItemContent> : AnyItem
onMove : OnMove.Callback? = nil,
onUpdate : OnUpdate.Callback? = nil
) {
assertIsValueType(Content.self)

self.content = content

if let sizing = sizing {
Expand Down