-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: traits exercises * remove generics in traits1 --------- Co-authored-by: Shramee Srivastav <[email protected]>
- Loading branch information
Showing
5 changed files
with
202 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Traits | ||
|
||
A trait is a collection of methods. | ||
|
||
Data types can implement traits. To do so, the methods making up the trait are defined for the data type. For example, the `u256` data type implements the `Into` trait. This allows a user to write `1.into()` to convert a felt into a u256. | ||
|
||
In this way, traits are somewhat similar to Java interfaces and C++ abstract classes. | ||
|
||
Because traits indicate shared behavior between data types, they are useful when writing generics. | ||
|
||
- [Traits & Impls](https://link.medium.com/IQGqboBerxb#83b5) |
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,33 @@ | ||
// traits1.cairo | ||
// Time to implement some traits! | ||
|
||
// Your task is to implement the trait | ||
// `AnimalTrait` for the type `Animal` | ||
// | ||
// Execute `starklings hint traits1` or use the `hint` watch subcommand for a hint. | ||
|
||
// Fill in the impl block to make the code work. | ||
|
||
// I AM NOT DONE | ||
|
||
#[derive(Copy, Drop)] | ||
struct Animal { | ||
noise: felt | ||
} | ||
|
||
trait AnimalTrait { | ||
fn new(noise: felt) -> Animal; | ||
fn make_noise(self: Animal) -> felt; | ||
} | ||
|
||
impl AnimalImpl of AnimalTrait {// TODO: implement the trait AnimalTrait for Animal | ||
} | ||
|
||
#[test] | ||
fn test_traits1() { | ||
// TODO make the test pass by creating two instances of Animal | ||
// and calling make_noise on them | ||
|
||
assert(cat.make_noise() == 'meow', 'Wrong noise'); | ||
assert(cow.make_noise() == 'moo', 'Wrong noise'); | ||
} |
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,46 @@ | ||
// traits2.cairo | ||
|
||
// The previous exercise did not make the distinction | ||
// between different types of animals, but this one does. | ||
// The trait `AnimalTrait` has two functions: | ||
// `new` and `make_noise`. | ||
// `new` should return a new instance of the type | ||
// implementing the trait. | ||
// `make_noise` should return the noise the animal makes. | ||
// The types `Cat` and `Cow` are already defined for you. | ||
// You need to implement the trait `AnimalTrait` for them. | ||
|
||
// No hints for this one! | ||
// Execute `starklings hint traits2` or use the `hint` watch subcommand for a hint. | ||
|
||
// I AM NOT DONE | ||
|
||
#[derive(Copy, Drop)] | ||
struct Cat { | ||
noise: felt, | ||
} | ||
|
||
#[derive(Copy, Drop)] | ||
struct Cow { | ||
noise: felt, | ||
} | ||
|
||
trait AnimalTrait<T> { | ||
fn new() -> T; | ||
fn make_noise(self: T) -> felt; | ||
} | ||
|
||
impl CatImpl of AnimalTrait::<Cat> { | ||
// TODO: implement the trait Animal for the type Cat | ||
} | ||
|
||
// TODO: implement the trait Animal for the type Cow | ||
|
||
#[test] | ||
fn test_traits2() { | ||
let kitty: Cat = AnimalTrait::new(); | ||
assert(kitty.make_noise() == 'meow', 'Wrong noise'); | ||
|
||
let cow: Cow = AnimalTrait::new(); | ||
assert(cow.make_noise() == 'moo', 'Wrong noise'); | ||
} |
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,78 @@ | ||
// traits3.cairo | ||
// | ||
// The previous exercise showed how to implement a trait for multiple types. | ||
// This exercise shows how you can implement multiple traits for a single type. | ||
// This is useful when you have types that share some common functionality, but | ||
// also have some unique functionality. | ||
|
||
// Execute `starklings hint traits3` or use the `hint` watch subcommand for a hint. | ||
|
||
// I AM NOT DONE | ||
|
||
#[derive(Copy, Drop)] | ||
struct Fish { | ||
noise: felt, | ||
distance: u32, | ||
} | ||
|
||
#[derive(Copy, Drop)] | ||
struct Dog { | ||
noise: felt, | ||
distance: u32, | ||
} | ||
|
||
trait AnimalTrait<T> { | ||
fn new() -> T; | ||
fn make_noise(self: T) -> felt; | ||
fn get_distance(self: T) -> u32; | ||
} | ||
|
||
trait FishTrait { | ||
fn swim(ref self: Fish) -> (); | ||
} | ||
|
||
trait DogTrait { | ||
fn walk(ref self: Dog) -> (); | ||
} | ||
|
||
impl AnimalFishImpl of AnimalTrait::<Fish> { | ||
fn new() -> Fish { | ||
Fish { noise: 'blub', distance: 0_u32 } | ||
} | ||
fn make_noise(self: Fish) -> felt { | ||
self.noise | ||
} | ||
fn get_distance(self: Fish) -> u32 { | ||
self.distance | ||
} | ||
} | ||
|
||
impl AnimalDogImpl of AnimalTrait::<Dog> { | ||
fn new() -> Dog { | ||
Dog { noise: 'woof', distance: 0_u32 } | ||
} | ||
fn make_noise(self: Dog) -> felt { | ||
self.noise | ||
} | ||
fn get_distance(self: Dog) -> u32 { | ||
self.distance | ||
} | ||
} | ||
|
||
// TODO: implement FishTrait for the type Fish | ||
|
||
// TODO: implement DogTrait for the type Dog | ||
|
||
#[test] | ||
fn test_traits3() { | ||
// Don't modify this test! | ||
let mut salmon: Fish = AnimalTrait::new(); | ||
salmon.swim(); | ||
assert(salmon.make_noise() == 'blub', 'Wrong noise'); | ||
assert(salmon.get_distance() == 1_u32, 'Wrong distance'); | ||
|
||
let mut dog: Dog = AnimalTrait::new(); | ||
dog.walk(); | ||
assert(dog.make_noise() == 'woof', 'Wrong noise'); | ||
assert(dog.get_distance() == 1_u32, 'Wrong distance'); | ||
} |
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