https://github.com/evuez/objex
:evergreen_tree: Having fun with macros.
https://github.com/evuez/objex
Last synced: 5 months ago
JSON representation
:evergreen_tree: Having fun with macros.
- Host: GitHub
- URL: https://github.com/evuez/objex
- Owner: evuez
- License: mit
- Created: 2018-01-06T18:41:42.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-07T09:16:55.000Z (about 8 years ago)
- Last Synced: 2024-10-19T17:30:23.556Z (about 1 year ago)
- Language: Elixir
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Objex
Just having fun with macros.
The code (in `lib/objex.ex`) is somewhat commented but it's my first time playing with
the AST so I'm not sure it's any good.
## Demo
```elixir
use Objex
require Logger
class Duck do
defm on_init(this) do
this.stamina = 5
this.speed = 10
this.x = 0
end
defm set_speed(this, speed) when speed > 0,
do: this.speed = speed
defm set_speed(this, _), do: Logger.error("Ducks can't go backward")
defm step(this) do
if this.stamina > 0 do
this.x = this.x + this.speed
this.stamina = this.stamina - 1
Logger.info("Duck is now at x=#{this.x}")
else
Logger.warn("Duck is too tired to go anywhere.")
end
end
end
```
```elixir
iex> duck = Duck.new()
iex> Duck.set_speed(duck, 4)
4
iex> Duck.step(duck)
[info] Duck is now at x=4
:ok
```
## Resources
- [The Erlangelist's macro series](http://theerlangelist.com/article/macros_1)
- [Compile-time work with Elixir macros](http://andrealeopardi.com/posts/compile-time-work-with-elixir-macros/)