2
lorentz
2y

Implement boolean as `HashSet<()>`

Comments
  • 1
    Implement () as HashSet<!>
  • 2
  • 1
    @Lensflare Set of unit has two states; it either contains unit or doesn't.

    Set of never has one state, empty.
  • 4
    @lorentz oh, I like this! Is it Rust? It’s quite similar to Swift and Kotlin.

    () == empty tuple == Unit == Void

    Never == Nothing

    Here is an implementation in Swift:

    extension Set where Element == Void {
    static var true: Self { [()] }
    static var false: Self { [] }
    }

    let myBoolValue: Set<()> = .true

    I could also add some operator overloading for negation and && and || but that’s trivial and boring.
  • 2
    @Lensflare nice, what about Set<Set<Nothing>>?
  • 2
    @Lensflare yes it is rust
  • 1
    @lorentz

    Implementation with Never.

    First I thought it won‘t be possible because you can‘t have instances of Never, but actually I don‘t need any. I just need an empty set of Set<Never>
  • 1
    Here is a slightly better looking solution:
  • 1
    I‘m having too much fun with it 😅
  • 0
    Kill it with volcanic burst?
Add Comment