Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wojtekmach/oop
OOP in Elixir!
https://github.com/wojtekmach/oop
elixir fun oop
Last synced: about 12 hours ago
JSON representation
OOP in Elixir!
- Host: GitHub
- URL: https://github.com/wojtekmach/oop
- Owner: wojtekmach
- Created: 2016-02-06T15:21:27.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-06-22T13:50:29.000Z (over 3 years ago)
- Last Synced: 2025-01-19T14:59:11.245Z (6 days ago)
- Topics: elixir, fun, oop
- Language: Elixir
- Size: 147 KB
- Stars: 315
- Watchers: 14
- Forks: 22
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - OOP in Elixir! (Examples and funny stuff)
- fucking-awesome-elixir - oop - OOP in Elixir! (Examples and funny stuff)
- awesome-dev-fun - OOP - OOP in Elixir! (Elixir)
- awesome-elixir - oop - OOP in Elixir. (Examples and funny stuff)
README
# OOP
[![Build Status](https://travis-ci.org/wojtekmach/oop.svg?branch=master)](https://travis-ci.org/wojtekmach/oop)
Are you tired of all of that modules, processes and functions nonsense? Do you want to just use classes, objects and methods? If so, use OOP [1] library in Elixir [2]!
## Demo
[![Lightning Talks - Wojtek Mach (ElixirConfEU 2016)](https://img.youtube.com/vi/5EtV2JUU0Z4/0.jpg)](https://www.youtube.com/watch?v=5EtV2JUU0Z4)
## Example
```elixir
import OOPclass Person do
var :namedef say_hello_to(who) do
what = "Hello #{who.name}"
IO.puts("#{this.name}: #{what}")
end
endjoe = Person.new(name: "Joe")
mike = Person.new(name: "Mike")
robert = Person.new(name: "Robert")joe.say_hello_to(mike) # Joe: Hello Mike
mike.say_hello_to(joe) # Mike: Hello Joe
mike.say_hello_to(robert) # Mike: Hello Robert
robert.say_hello_to(mike) # Robert: Hello Mikejoe.set_name("Hipster Joe")
joe.name # => Hipster Joe
```An OOP library wouldn't be complete without inheritance:
```elixir
class Animal do
var :name
endclass Dog < Animal do
var :breed
endsnuffles = Dog.new(name: "Snuffles", breed: "Shih Tzu")
snuffles.name # => "Snuffles"
snuffles.breed # => "Shih Tzu"
```... or multiple inheritance:
```elixir
class Human do
var :name
endclass Horse do
var :horseshoes_on?
endclass Centaur < [Human, Horse] do
endjohn = Centaur.new(name: "John", horseshoes_on?: true)
john.name # => "John"
john.horseshoes_on? # => true
```See more usage in the [test suite](test/oop_test.exs).
## Installation
Add `oop` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:oop, "~> 0.1.0"}]
end
```[1] According to Alan Kay, the inventor of OOP, "objects" is the lesser idea; the big idea is "messaging". In that sense, I can't agree more with Joe Armstrong's quote that Erlang is "possibly the only object-oriented language".
[2] Please don't. You've been warned.
## License
The MIT License (MIT)
Copyright (c) 2015 Wojciech Mach
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.