Skip to content

Commit

Permalink
deploy: 7a3ab2b
Browse files Browse the repository at this point in the history
  • Loading branch information
damirka committed Oct 21, 2024
1 parent 84dfccb commit 57d4156
Show file tree
Hide file tree
Showing 26 changed files with 1,209 additions and 1,757 deletions.
48 changes: 4 additions & 44 deletions move-basics/comments.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,65 +201,25 @@ <h1 id="comments"><a class="header" href="#comments">Comments</a></h1>
documentation. There are three types of comments in Move: line comment, block comment, and doc
comment.</p>
<h2 id="line-comment"><a class="header" href="#line-comment">Line comment</a></h2>
<pre><code class="language-Move">module book::comments_line {
fun some_function() {
// this is a comment line
}
}
<pre><code class="language-Move">{{#include ../../../packages/samples/sources/move-basics/comments.move:line}}
</code></pre>
<p>You can use double slash <code>//</code> to comment out the rest of the line. Everything after <code>//</code> will be
ignored by the compiler.</p>
<pre><code class="language-Move">module book::comments_line_2 {
// let's add a note to everything!
fun some_function_with_numbers() {
let a = 10;
// let b = 10 this line is commented and won't be executed
let b = 5; // here comment is placed after code
a + b; // result is 15, not 10!
}
}
<pre><code class="language-Move">{{#include ../../../packages/samples/sources/move-basics/comments.move:line_2}}
</code></pre>
<h2 id="block-comment"><a class="header" href="#block-comment">Block comment</a></h2>
<p>Block comments are used to comment out a block of code. They start with <code>/*</code> and end with <code>*/</code>.
Everything between <code>/*</code> and <code>*/</code> will be ignored by the compiler. You can use block comments to
comment out a single line or multiple lines. You can even use them to comment out a part of a line.</p>
<pre><code class="language-Move">module book::comments_block {
fun /* you can comment everywhere */ go_wild() {
/* here
there
everywhere */ let a = 10;
let b = /* even here */ 10; /* and again */
a + b;
}
/* you can use it to remove certain expressions or definitions
fun empty_commented_out() {

}
*/
}
<pre><code class="language-Move">{{#include ../../../packages/samples/sources/move-basics/comments.move:block}}
</code></pre>
<p>This example is a bit extreme, but it shows how you can use block comments to comment out a part of
a line.</p>
<h2 id="doc-comment"><a class="header" href="#doc-comment">Doc comment</a></h2>
<p>Documentation comments are special comments that are used to generate documentation for your code.
They are similar to block comments, but they start with three slashes <code>///</code> and are placed before
the definition of the item they document.</p>
<pre><code class="language-Move">/// Module has documentation!
module book::comments_doc {

/// This is a 0x0 address constant!
const AN_ADDRESS: address = @0x0;

/// This is a struct!
public struct AStruct {
/// This is a field of a struct!
a_field: u8,
}

/// This function does something!
/// And it's documented!
fun do_something() {}
}
<pre><code class="language-Move">{{#include ../../../packages/samples/sources/move-basics/comments.move:doc}}
</code></pre>
<!-- TODO: docgen, which members are in the documentation -->

Expand Down
41 changes: 3 additions & 38 deletions move-basics/constants.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,38 +205,14 @@ <h1 id="constants"><a class="header" href="#constants">Constants</a></h1>
give names to static values that are used throughout a module. For example, if there's a default
price for a product, you might define a constant for it. Constants are stored in the module's
bytecode, and each time they are used, the value is copied.</p>
<pre><code class="language-move">module book::shop_price {
use sui::coin::Coin;
use sui::sui::SUI;

/// The price of an item in the shop.
const ITEM_PRICE: u64 = 100;
/// The owner of the shop, an address.
const SHOP_OWNER: address = @0xa11ce;

/// An item sold in the shop.
public struct Item { /* ... */ }

/// Purchase an item from the shop.
public fun purchase(coin: Coin&lt;SUI&gt;): Item {
assert!(coin.value() == ITEM_PRICE, 0);

transfer::public_transfer(coin, SHOP_OWNER);

Item { /* ... */ }
}
}
<pre><code class="language-move">
</code></pre>
<h2 id="naming-convention"><a class="header" href="#naming-convention">Naming Convention</a></h2>
<p>Constants must start with a capital letter - this is enforced at the compiler level. For constants
used as a value, there's a convention to use uppercase letters and underscores to separate words.
It's a way to make constants stand out from other identifiers in the code. One exception is made for
<a href="./assert-and-abort.html#assert-and-abort">error constants</a>, which are written in ECamelCase.</p>
<pre><code class="language-move">/// Price of the item used at the shop.
const ITEM_PRICE: u64 = 100;

/// Error constant.
const EItemNotFound: u64 = 1;
<pre><code class="language-move">
</code></pre>
<h2 id="constants-are-immutable"><a class="header" href="#constants-are-immutable">Constants are Immutable</a></h2>
<p>Constants can't be changed and assigned new values. They are part of the package bytecode, and
Expand All @@ -254,18 +230,7 @@ <h2 id="using-config-pattern"><a class="header" href="#using-config-pattern">Usi
<p>A common use case for an application is to define a set of constants that are used throughout the
codebase. But due to constants being private to the module, they can't be accessed from other
modules. One way to solve this is to define a &quot;config&quot; module that exports the constants.</p>
<pre><code class="language-move">module book::config {
const ITEM_PRICE: u64 = 100;
const TAX_RATE: u64 = 10;
const SHIPPING_COST: u64 = 5;

/// Returns the price of an item.
public fun item_price(): u64 { ITEM_PRICE }
/// Returns the tax rate.
public fun tax_rate(): u64 { TAX_RATE }
/// Returns the shipping cost.
public fun shipping_cost(): u64 { SHIPPING_COST }
}
<pre><code class="language-move">
</code></pre>
<p>This way other modules can import and read the constants, and the update process is simplified. If
the constants need to be changed, only the config module needs to be updated during the package
Expand Down
Loading

0 comments on commit 57d4156

Please sign in to comment.