Postfix type erasure
25 Mar 2020 ⇐ Notes archive(This is an entry in my technical notebook. There will likely be typos, mistakes, or wider logical leaps — the intent here is to “let others look over my shoulder while I figure things out.”)
A belated entry on an operator I posted before…all of this (gestures wildly) started.
⬦
There’s nuance in determining whether or not to type erase a publisher—my next longer-form post will cover this—but when you need to, eraseToAnyPublisher()
’s ergonomics aren’t great.
It requires 22 characters (including a dot for the method call and Void
argument) to apply a rather one-character concept.
And I know operators are borderline #holy-war—still, if you’re open to them, I’ve borrowed prior art from Bow and Point-Free by using a ^
postfix operator.
It passes the three checks any operator should.
- Does the operator overload an existing one in Swift? Thankfully not (since bitwise XOR is infix).
- Does its shape convey its intent? To an extent! The caret hints at a sort of “lifting” and that’s what erasure is after all. Removing specific details, leaving behind a more general shape.
- Does it have prior art? Yep.
- Bow defines one to ease higher-kinded type emulation.
- Point-Free defined a prefix variant to lift key path expressions into function form before SE-0249 landed in Swift 5.2
The operator has tidied the Combine I’ve written so far. Here’s a gist with its definition.