Skip to content

Commit

Permalink
Merge pull request #243 from kyleve/kve/no-more-classes-pls
Browse files Browse the repository at this point in the history
Forbid class types for ItemContent and HeaderFooterContent
  • Loading branch information
kyleve authored Dec 17, 2020
2 parents dee0a9b + 7e3a797 commit abd0b80
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
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

0 comments on commit abd0b80

Please sign in to comment.