This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
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
Braiden Vasco
committed
Oct 28, 2015
1 parent
7e4924d
commit f7ce970
Showing
2 changed files
with
89 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# This comes from Rust traits example | ||
# http://rustbyexample.com/trait.html | ||
|
||
Animal = Typeclass.new a: Object do | ||
fn :name, [:a] | ||
fn :noise, [:a] | ||
|
||
fn :talk, [:a] do |a| | ||
"#{name a} says \"#{noise a}\"" | ||
end | ||
end | ||
|
||
Dog = Struct.new(:name) | ||
|
||
Typeclass.instance Animal, a: Dog do | ||
def name(a) | ||
a.name | ||
end | ||
|
||
def noise(_a) | ||
'woof woof!' | ||
end | ||
end | ||
|
||
Sheep = Struct.new(:name) | ||
|
||
Typeclass.instance Animal, a: Sheep do | ||
def name(a) | ||
a.name | ||
end | ||
|
||
def noise(_a) | ||
'baaah' | ||
end | ||
end | ||
|
||
include Animal | ||
|
||
dog = Dog['Spike'] | ||
sheep = Sheep['Dolly'] | ||
|
||
puts talk(dog) | ||
puts talk(sheep) |