Bahn-X / swift-composable-navigator

An open source library for building deep-linkable SwiftUI applications with composition, testing and ergonomics in mind
MIT License
581 stars 25 forks source link

Introduce PathBuilders namespace and PathBuilder protocol #4

Closed ohitsdaniel closed 3 years ago

ohitsdaniel commented 3 years ago

As the avoiding erasing to AnyView lead to PathBuilder being a generic struct, we would have to define the for returns which can be tedious.

Instead, we'll leverage opaque result types and use the some keyword in factory functions returning PathBuilders.

Had to introduce the PathBuilders namespace enum, as static members cannot be used on protocol metatypes. :(

Check out this example regarding this issue 🔝

protocol Builder {
  associatedtype Content
  func build() -> Content
}

struct B<Content>: Builder {
  let _build: () -> Content

  func build() -> Content {
    _build()
  }
}

extension Builder {
  static func builder() -> some Builder {
    B(_build: {1})
  }
}

let f = { Builder.builder() }

Comes with the drawback, that we now have to type PathBuilders.screen etc. whenever we want to instantiate a particular path builder, but that's better than erasing to AnyView.