Skip to content

Latest commit

 

History

History
49 lines (39 loc) · 929 Bytes

File metadata and controls

49 lines (39 loc) · 929 Bytes

TypeErasureMacro

A simple & quick way to create a type eraser for your custom Protocols.

Usage

Custom Protocol with @TypeErasure

@TypeErasure
protocol ContentDrawable {
    var size: CGSize { get }
    var backgroundColor: Color { get }
    
    func draw()
}

Expanded Code

protocol ContentDrawable {
    var size: CGSize { get }
    var backgroundColor: Color { get }
    
    func draw()
}

struct AnyContentDrawable : ContentDrawable  {
    private var _size: CGSize
    var size: CGSize  {
        _size
    }
    private var _backgroundColor: Color
    var backgroundColor: Color  {
        _backgroundColor
    }
    private var _draw: () -> Void
    func draw() {
        _draw()
    }
    init(_ contentdrawable: ContentDrawable ) {
        _size = contentdrawable.size
        _backgroundColor = contentdrawable.backgroundColor
        _draw = contentdrawable.draw
    }
}