Skip to content

Commit

Permalink
implement method syntax concept
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNeshi committed Dec 30, 2024
1 parent 7f33be6 commit b032878
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
6 changes: 2 additions & 4 deletions concepts/method-syntax/.meta/config.json
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": []
}
60 changes: 60 additions & 0 deletions concepts/method-syntax/about.md
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.
4 changes: 4 additions & 0 deletions concepts/method-syntax/introduction.md
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.
7 changes: 6 additions & 1 deletion concepts/method-syntax/links.json
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"
}
]

0 comments on commit b032878

Please sign in to comment.