/struct-oriented-programming

Presentation for Podlodka iOS Crew #9 - Modern iOS Coding

Primary LanguageSwift

Struct oriented programming vs Protocol oriented programming

  • How do we use protocols
  • How to do it without protocols
  • How to transform any protocol to a sturct
  • Porotocol's limitations
  • Struct oriented programming's limitations
  • Play with uikit animations

Protocol oriented programming

protocol Animator {
    associatedtype Target
    static func animate(
        _ target: Target,
        completion: ((Bool) -> Void)?
    )
}

struct FadeInAnimator: AnimatorProtocol {
    static func animate(
        _ target: UIView,
        completion: ((Bool) -> Void)?
    ) {
        UIView.animate(
            withDuration: 5,
            delay: 0,
            options: [],
            animations: { target.alpha = 0.0 },
            completion: completion
        )
    }
}

Struct oriented programming

struct Animator<Target> {
    typealias Completion = (Bool) -> Void
    var animate: (Target, _ completion: Completion?) -> Void
}

extension Animator where Target == UIView {
    static func fadeOut(
        params: UIViewAnimationParams
    ) -> Animator {
        Animator { view, completion in
            UIView.animate(
                withDuration: params.duration,
                delay: params.delay,
                options: params.options,
                animations: { view.alpha = 0.0 },
                completion: completion
            )
        }
    }
}

Where does it use?

There are two main entites which you should look at:

Diffing - allows to compare Values and convert them to and from Data

Snapshotting - allows to transform a snapshottable value into a diffable format (like text or an image) for snapshot testing.

More about it:

Resources