https://github.com/mauro3/oo.jl
OO for Julia
https://github.com/mauro3/oo.jl
Last synced: 4 months ago
JSON representation
OO for Julia
- Host: GitHub
- URL: https://github.com/mauro3/oo.jl
- Owner: mauro3
- License: other
- Created: 2017-12-08T09:31:16.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-02-08T16:03:14.000Z (over 5 years ago)
- Last Synced: 2024-10-12T10:31:17.513Z (9 months ago)
- Language: Julia
- Size: 4.88 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# OO
Satirical take on the new `getproperty` method of Julia 0.7.
**Please, do not use it!**Refs:
- [#1974](https://github.com/JuliaLang/julia/issues/1974)
- [#24960](https://github.com/JuliaLang/julia/pull/24960)```julia
using OO# OO-ify Array
# Aside: it is not possible to automatically do this for all types as
# redefining Base.getproperty(self, p::Symbol) seg-faults Julia.
@OO Array
[1,2].length # ->2
[1,2].sum # ->3@OO abstract type Animal end
_getproperty(self::Animal, ::Val{:eat}) = food -> println("$self eats $food")
@method Animal.eat(self, food) = println("$self eats $food")
@classmethod Animal.notsurewhat(arg) = println("This is a class-method saying: $arg")@OO struct Dog <: Animal
name
breed
color
sound
end
Base.show(io::IO, d::Dog) = println(io, "Dog $(d.name)")
@method Dog.bark(self) = println("$(self.sound)")
@method Dog.bite(self, other) = println("Dog $(self.name) bites $other")maclary = Dog("Hairy Maclary", "Terrier", "Black", "Yep yep yep yep")
maclary.bark() # Yep yep yep yep
maclary.bite("me") # Dog Hairy Maclary bites me
maclary.eat("dog food") # Dog Hairy Maclary eats dog food
maclary.notsurewhat("Hi MacLary") # This is a class-method saying: Hi MacLary
```