-
-
Notifications
You must be signed in to change notification settings - Fork 12
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
Showing
4 changed files
with
72 additions
and
5 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 |
---|---|---|
@@ -1,7 +1,5 @@ | ||
{ | ||
"blurb": "<todo>", | ||
"authors": [ | ||
"<your_gh_username>" | ||
], | ||
"blurb": "In Cairo, methods and associated functions allow you to organize functionality around specific types, making your code modular and intuitive.", | ||
"authors": ["0xNeshi"], | ||
"contributors": [] | ||
} |
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 |
---|---|---|
@@ -1 +1,61 @@ | ||
# Method Syntax | ||
|
||
Methods in Cairo are similar to functions, but they are tied to a specific type through traits. | ||
|
||
Their first parameter is always `self`, representing the instance on which the method is called. | ||
|
||
While Cairo doesn’t allow defining methods directly on a type, you can achieve the same functionality by defining a trait and implementing it for the type. | ||
|
||
Here’s an example of defining a method on a `Rectangle` type using a trait: | ||
|
||
```rust | ||
#[derive(Copy, Drop)] | ||
struct Rectangle { | ||
width: u64, | ||
height: u64, | ||
} | ||
|
||
#[generate_trait] | ||
impl RectangleImpl of RectangleTrait { | ||
fn area(self: @Rectangle) -> u64 { | ||
(*self.width) * (*self.height) | ||
} | ||
} | ||
|
||
fn main() { | ||
let rect = Rectangle { width: 30, height: 50 }; | ||
println!("Area is {}", rect.area()); | ||
} | ||
``` | ||
|
||
In the example above, the `area` method calculates the area of a rectangle. | ||
|
||
Using the `#[generate_trait]` attribute simplifies the process by automatically creating the required trait for you. | ||
|
||
This makes your code cleaner while still allowing methods to be associated with specific types. | ||
|
||
## Associated Functions | ||
|
||
Associated functions are similar to methods but don’t operate on an instance of a type—they don’t take `self` as a parameter. | ||
|
||
These functions are often used as constructors or utility functions tied to the type. | ||
|
||
```rust | ||
#[generate_trait] | ||
impl RectangleImpl of RectangleTrait { | ||
fn square(size: u64) -> Rectangle { | ||
Rectangle { width: size, height: size } | ||
} | ||
} | ||
|
||
fn main() { | ||
let square = RectangleTrait::square(10); | ||
println!("Square dimensions: {}x{}", square.width, square.height); | ||
} | ||
``` | ||
|
||
Associated functions, like `Rectangle::square`, use the `::` syntax and are namespaced to the type. | ||
|
||
They make it easy to create or work with instances without requiring an existing object. | ||
|
||
By organizing related functionality into traits and implementations, Cairo enables clean, modular, and extensible code structures. |
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 |
---|---|---|
@@ -1 +1,5 @@ | ||
# Introduction | ||
|
||
In Cairo, methods and associated functions allow you to organize functionality around specific types, making your code modular and intuitive. | ||
Methods operate on an instance of a type and always include `self` as their first parameter, while associated functions, such as constructors, don’t require an instance. | ||
Together, they enable a clean and structured approach to defining behavior and utilities for your types. |
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 |
---|---|---|
@@ -1 +1,6 @@ | ||
[] | ||
[ | ||
{ | ||
"url": "https://book.cairo-lang.org/ch05-03-method-syntax.html", | ||
"description": "Method Syntax in The Cairo Book" | ||
} | ||
] |