-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a27c32
commit 35e064c
Showing
4 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
Zeros.@pirate Base | ||
Extend some functions in Base to return `Zero()` or `One()`, even when called without | ||
any arguments of types "owned" by Zeros.jl. (This is sometimes referred to as "type piracy".) | ||
The function calls s `+()` and `*()` (without arguments) will return `Zero()` and `One()` | ||
respectively. The same will be true for `zero()` and `one()` without arguments. | ||
The functions `zero(Any)` and `one(Any)` will also return `Zero()` and `One()`, respectively, | ||
which in turn will make `sum([])` and `prod([])` return those values. | ||
Finally, `sin(π)` and `tan(π)` will return `Zero()`, fixing a bug in Base. | ||
""" | ||
macro pirate(m::Symbol) | ||
esc(pirate_code(Val(m))) | ||
end | ||
|
||
pirate_code(::Val{:Base}) = quote | ||
Base.:+() = Zero() | ||
Base.:*() = One() | ||
Base.zero() = Zero() | ||
Base.one() = One() | ||
Base.zero(::Type{Any}) = Zero() | ||
Base.one(::Type{Any}) = One() | ||
Base.sin(::Irrational{:π}) = Zero() | ||
Base.tan(::Irrational{:π}) = Zero() | ||
end |
This file contains 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