Created
November 2, 2024 15:10
-
-
Save oschulz/d576a377899a798675de1c85eb2a1f8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract type Countable{sym} end | |
| Base.show(io::IO, x::Countable{sym}) where sym = print(io, x.n == 1 ? "$sym" : "$(x.n)$sym") | |
| getsym(x::Countable{sym}) where sym = sym | |
| Base.:*(n::Integer, x::Countable) = typeof(x)(n * x.n) | |
| Base.:+(a::Countable, b::Countable) = error("Can't add $(getsym(a))s and $((getsym(b)))s") | |
| Base.:+(a::T, b::T) where {T<:Countable} = T(a.n + b.n) | |
| Base.:<(a::Countable, b::Countable) = error("Can't compare $(getsym(a))s with $(getsym(b))s") | |
| Base.:<(a::T, b::T) where {T<:Countable} = a.n < b.n | |
| struct Fruit{sym} <: Countable{sym} | |
| n::Int | |
| end | |
| struct Animals{sym} <: Countable{sym} | |
| n::Int | |
| end | |
| Base.:+(eaters::Animals, fruit::Fruit) = typeof(fruit)(fruit.n - eaters.n) | |
| const ๐ = Fruit{:๐}(1) | |
| const ๐ = Fruit{:๐}(1) | |
| const ๐ = Fruit{:๐}(1) | |
| const ๐ฎ = Animals{:๐ฎ}(1) | |
| const ๐ = Animals{:๐}(1) | |
| const ๐ = Animals{:๐}(1) | |
| Base.:+(eaters::Animals{:๐}, fruit::Fruit{:๐}) = typeof(fruit)(fruit.n - 2 * eaters.n) | |
| function Base.:+(eaters::Animals{:๐}, fruit::Fruit) | |
| @warn "Moooooo!" | |
| return fruit | |
| end | |
| typeof(๐) == typeof(๐) | |
| ๐ + 2๐ + 3๐ | |
| 2๐ < 3๐ | |
| 2๐ + 3๐ | |
| ๐ < ๐ | |
| 4๐ฎ + 6๐ | |
| 2๐ฎ + 6๐ | |
| 2๐ + 6๐ | |
| 2๐ + 6๐ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment