Skip to content

LiYanan2004/TypeErasureMacro

Repository files navigation

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
    }
}

About

Adding Type Eraser for your custom Protocols with simple Macro.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages