Last active
January 23, 2025 02:20
-
-
Save Overbryd/d1338ad283ddc9bc6967a3c55d86ecac 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
defmodule Decimal.Prefix do | |
defmacro __using__(_args) do | |
quote do | |
import Decimal.Prefix, only: [decimal: 1] | |
end | |
end | |
def overrides, do: ["+": 2, "-": 2, "*": 2, "/": 2] | |
def a + b do | |
Decimal.add(a, b) | |
end | |
def a - b do | |
Decimal.sub(a, b) | |
end | |
def a * b do | |
Decimal.mult(a, b) | |
end | |
def a / b do | |
Decimal.div(a, b) | |
end | |
defmacro decimal(code) do | |
enclosed do | |
quote do | |
import Kernel, except: unquote(Decimal.Prefix.overrides()) | |
import Decimal.Prefix | |
unquote(code) | |
end | |
end | |
end | |
defp enclosed(do: code), do: code | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment