Skip to content

Instantly share code, notes, and snippets.

@rational-kunal
Created May 2, 2025 12:40
Show Gist options
  • Select an option

  • Save rational-kunal/96ddaf8f0da2cd03f4edac190f2263ce to your computer and use it in GitHub Desktop.

Select an option

Save rational-kunal/96ddaf8f0da2cd03f4edac190f2263ce to your computer and use it in GitHub Desktop.
enum πŸ“¦<Value> {
case 😺(Value)
case 😡
init(_ from: Value) {
self = .😺(from)
}
}
extension πŸ“¦: ExpressibleByNilLiteral {
init(nilLiteral: ()) {
self = .😡
}
}
postfix operator ❓
postfix func ❓<T>(value: T) -> πŸ“¦<T> {
.😺(value)
}
postfix operator ❗
postfix func ❗<T>(value: πŸ“¦<T>) -> T {
switch value {
case .😺(let v):
return v
case .😡:
fatalError("Cat is dead Bruh!")
}
}
infix operator β“βž‘οΈ: NilCoalescingPrecedence
func β“βž‘οΈ<T>(lhs: πŸ“¦<T>, rhs: @autoclosure () -> T) -> T {
switch lhs {
case .😺(let value):
return value
case .😡:
return rhs()
}
}
@dynamicMemberLookup
struct _πŸ“¦<Value> {
private let storage: πŸ“¦<Value>
init(from: πŸ“¦<Value>) {
storage = from
}
subscript<T>(dynamicMember keyPath: KeyPath<Value, πŸ“¦<T>>) -> πŸ“¦<T> {
switch storage {
case .😺(let v):
return v[keyPath: keyPath]
case .😡:
return .😡
}
}
}
postfix operator ❔
postfix func ❔<T>(value: πŸ“¦<T>) -> _πŸ“¦<T> {
return _πŸ“¦(from: value)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment