From b032878de3bbff6c90f2274cc3db78e4f22290a8 Mon Sep 17 00:00:00 2001 From: Nenad Date: Mon, 30 Dec 2024 21:14:32 +0100 Subject: [PATCH] implement method syntax concept --- concepts/method-syntax/.meta/config.json | 6 +-- concepts/method-syntax/about.md | 60 ++++++++++++++++++++++++ concepts/method-syntax/introduction.md | 4 ++ concepts/method-syntax/links.json | 7 ++- 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/concepts/method-syntax/.meta/config.json b/concepts/method-syntax/.meta/config.json index 35b1d32b..967eec7f 100644 --- a/concepts/method-syntax/.meta/config.json +++ b/concepts/method-syntax/.meta/config.json @@ -1,7 +1,5 @@ { - "blurb": "", - "authors": [ - "" - ], + "blurb": "In Cairo, methods and associated functions allow you to organize functionality around specific types, making your code modular and intuitive.", + "authors": ["0xNeshi"], "contributors": [] } diff --git a/concepts/method-syntax/about.md b/concepts/method-syntax/about.md index 5dfaae2c..7f81fd03 100644 --- a/concepts/method-syntax/about.md +++ b/concepts/method-syntax/about.md @@ -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. diff --git a/concepts/method-syntax/introduction.md b/concepts/method-syntax/introduction.md index e10b99d0..46857542 100644 --- a/concepts/method-syntax/introduction.md +++ b/concepts/method-syntax/introduction.md @@ -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. diff --git a/concepts/method-syntax/links.json b/concepts/method-syntax/links.json index fe51488c..8045e5b7 100644 --- a/concepts/method-syntax/links.json +++ b/concepts/method-syntax/links.json @@ -1 +1,6 @@ -[] +[ + { + "url": "https://book.cairo-lang.org/ch05-03-method-syntax.html", + "description": "Method Syntax in The Cairo Book" + } +]