Using Type Markers for ‘is’ Type Predicates to Cover Generic Types

Maybe you have some old code (you wouldn’t write this nowadays, of course) that uses someValue is SomeType checks. Maybe you need to check if someValue is a generic type, like the following Box: Then your is predicates won’t work: The good news is that since you treat the concrete type as a mere marker, you can express this condition with a type marker protocol:

Continue reading …

Dependency Injection via Protocol Composition

Watching Stephen Celis’ “How to Control the World”, NSSpain 2018 presentation, one of the common Swift/iOS patterns he brings up caught my attention.

It’s this piece of code:

protocol APIClientProvider {
  var api: APIClientProtocol { get }
}

protocol DateProvider {
  func date() -> Date
}

extension World: APIClientProvider, DateProvider {}

class MyViewController: UIViewController {
  typealias Dependencies = APIClientProvider & DateProvider

  let label = UILabel()
  let dependencies: Dependencies

  init(dependencies: Dependencies) {
    self.dependencies = dependencies
  }

  func greet() {
    self.dependencies.api.fetchCurrentUser { result in
      if let user = result.success {
        self.label.text = "Hi, \(user.name)! It’s \(self.dependencies.date())."
      }
    }
  }
}

I never saw usage of a typealias Dependencies declaration that uses Protocol Composition to declare which dependencies are needed (expressed as 1 type being the combination of all actual dependencies).

This was news to me, so I wonder if you ran into something like this out there in the wild.

Show Referrers in TelemetryDeck for Web with a TopN TQL Query

The current TelemetryDeck for web setup guide (version of 2023-12-01) is quite simple. Insert this into your HTML, and you’re set: From that point onward, you’ll be collecting signals per web site hit. Internally, the signal’s type is "type":"pageview". These signals will include all the usual suspects: browser, os and version, url (the page that was visited), and also referrer (where visitors may have come from, following a link).

Continue reading …

How Long Does a Disk Preclear in Unraid Take for 12TB of Data?

I’m upgrading my 4TB drives in the NAS to 12TB drives. “Let’s be clever,” I thought (foreshadowing), “and do two things in parallel!” So installing the next drive into the disk array of my Unraid NAS, I let Unraid do these tasks: Does that speed things up, though? No, not at all, because Preclearing a disk takes 3x as long: Around 21 hours for each step that checks every nook and cranny of the HDD.

Continue reading …