7po3j-syaaa-aaaal-qbqea-cai.icp0.io Open in urlscan Pro
2a00:fb01:400:200:5000:61ff:fe45:43ab  Public Scan

URL: https://7po3j-syaaa-aaaal-qbqea-cai.icp0.io/common-programming-concepts/objects-and-classes/classes.html
Submission: On December 24 via api from US — Scanned from CH

Form analysis 1 forms found in the DOM

<form id="searchbar-outer" class="searchbar-outer">
  <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>

Text Content

 1.  The Motoko Programming Language
 2.  
 3.  Part 1
 4.  1. Introduction
     ❱
 5.  1. 1.1. Getting Started
 6.  2. Common Programming Concepts
     ❱
 7.  1.  2.1. Variables
     2.  2.2. Mutability
     3.  2.3. Comments
     4.  2.4. Types
         ❱
     5.  1. 2.4.1. Tuples
         2. 2.4.2. Records
         3. 2.4.3. Variants
         4. 2.4.4. Immutable Arrays
         5. 2.4.5. Mutable Arrays
     6.  2.5. Operators
         ❱
     7.  1. 2.5.1. Numeric operators
         2. 2.5.2. Relational operators
         3. 2.5.3. Assignment operators
         4. 2.5.4. Text concatenation
         5. 2.5.5. Logical expressions
         6. 2.5.6. Bitwise operators
         7. 2.5.7. Operator precedence
     8.  2.6. Pattern Matching
     9.  2.7. Functions
     10. 2.8. Options and Results
     11. 2.9. Control Flow
         ❱
     12. 1. 2.9.1. If Expression
         2. 2.9.2. If Else Expression
         3. 2.9.3. Switch Expression
     13. 2.10. Objects and Classes
         ❱
     14. 1. 2.10.1. Objects
         2. 2.10.2. Classes
     15. 2.11. Modules and Imports
     16. 2.12. Assertions
 8.  3. Internet Computer Programming Concepts
     ❱
 9.  1. 3.1. Actors
        ❱
     2. 1. 3.1.1. From Actor to Canister
        2. 3.1.2. Canister Calls from Clients
     3. 3.2. Principals and Authentication
     4. 3.3. Async Data
        ❱
     5. 1. 3.3.1. Shared Types
        2. 3.3.2. Candid
     6. 3.4. Basic Memory Persistence
        ❱
     7. 1. 3.4.1. Upgrades
        2. 3.4.2. Stable Variables
 10. 
 11. Part 2
 12. 4. Advanced Types
     ❱
 13. 1. 4.1. Generic Types
     2. 4.2. Subtyping
     3. 4.3. Recursive Types
     4. 4.4. Type Bounds
 14. 5. The Base Library
     ❱
 15. 1. 5.1. Primitive Types
        ❱
     2. 1.  5.1.1. Bool
        2.  5.1.2. Nat
        3.  5.1.3. Int
        4.  5.1.4. Float
        5.  5.1.5. Principal
        6.  5.1.6. Text
        7.  5.1.7. Char
        8.  5.1.8. Bounded Number Types
            ❱
        9.  1. 5.1.8.1. Nat8
            2. 5.1.8.2. Nat16
            3. 5.1.8.3. Nat32
            4. 5.1.8.4. Nat64
            5. 5.1.8.5. Int8
            6. 5.1.8.6. Int16
            7. 5.1.8.7. Int32
            8. 5.1.8.8. Int64
        10. 5.1.9. Blob
     3. 5.2. Utility Modules
        ❱
     4. 1. 5.2.1. Iterators
        2. 5.2.2. Hash
        3. 5.2.3. Option
        4. 5.2.4. Result
        5. 5.2.5. Order
        6. 5.2.6. Error
        7. 5.2.7. Debug
     5. 5.3. Data Structures
        ❱
     6. 1. 5.3.1. Array
        2. 5.3.2. List
        3. 5.3.3. Buffer
        4. 5.3.4. HashMap
        5. 5.3.5. RBTree
     7. 5.4. More Data Structures
     8. 5.5. IC APIs
        ❱
     9. 1. 5.5.1. Time
        2. 5.5.2. Timer
        3. 5.5.3. CertifiedData
        4. 5.5.4. Random
        5. 5.5.5. Experimental
 16. 6. Advanced Concepts
     ❱
 17. 1. 6.1. Async Programming
     2. 6.2. Scalability
        ❱
     3. 1. 6.2.1. Actor Classes
        2. 6.2.2. Stable Storage
     4. 6.3. System API's
        ❱
     5. 1. 6.3.1. Message Inspection
        2. 6.3.2. Timers
        3. 6.3.3. Certified Variables
        4. 6.3.4. Pre-upgrade and Post-upgrade
        5. 6.3.5. Cryptographic Randomness
 18. 
 19. Part 3
 20. 7. Project Deployment
     ❱
 21. 1. 7.1. Installing the SDK
     2. 7.2. Local Deployment
     3. 7.3. Canister Status
     4. 7.4. Identities and PEM Files
     5. 7.5. Cycles and ICP
     6. 7.6. Cycles Wallet
     7. 7.7. IC Deployment
 22. 8. Common Internet Computer Canisters
     ❱
 23. 1. 8.1. IC Management Canister
     2. 8.2. ICP Ledger Canister
     3. 8.3. Cycle Minting Canister
 24. 9. Internet Computer Standards
     ❱
 25. 1. 9.1. ICRC1
 26. 10. Tokenized Comments Example
 27. 
 28. APPENDIX
 29. 11. TABLES


 * Light
 * Rust
 * Coal
 * Navy
 * Ayu


THE MOTOKO PROGRAMMING LANGUAGE BOOK





CLASSES

Classes in essence are nothing more than a function with a fancy name and
special notation. A class, like any other function, takes in values of a certain
type and returns a value of a certain type. The return type of a class is always
an object. Classes are like 'factories' or 'templates' for objects.

Consider the following two type declarations:

type SomeObject = {};

type SomeClass = () -> SomeObject;


The first is the type of an empty object. The second is the type of a class.
It's a function type that could take in a number of arguments and return an
instance of an object of type SomeObject.

But classes have a special notation using the class keyword. We declare a class
like this:

class MyClass() {
    private let a = 0;
    public let b = 1;
};


To use this class we have to create an instance of this class:

let myClassInstance = MyClass();


When we instantiate our class by calling MyClass() it returns an object. That
object is now named myClassInstance.

In fact, we could set the expected type of the returned object by defining the
type and annotating our variable with it:

type ObjectType = {
    b : Nat;
};

let anotherClassInstance : ObjectType = MyClass();


Now this class is not very useful and we could just have declared an object
instead of declaring a class and instantiating it.

Let declare a class that takes some arguments, instantiate two objects with it
and use those objects:

class CryptoAccount(amount : Nat, multiplier : Nat) {
    private func calc(a : Nat, b : Nat) : Nat {
        a * b;
    };

    public var balance = calc(amount, multiplier);
};

let account1 = CryptoAccount(10, 5);
let account2 = CryptoAccount(10, 10);

account1.balance += 50;
account2.balance += 20;


Lets analyze the code line by line. Our class CryptoAccount takes in two values
of type Nat. These are used once during initialization.

The first member of our class is a private function named calc. It just takes
two values of type Nat and returns their product. The second member of the class
is a mutable variable named balance which is declared by calling calc with the
two values coming from our class.

Because this class only has one public field, the expected type of the object
that is returned should be { var balance : Nat }.

We then define two objects account1 and account2 by calling our class with
different values. Both objects have a mutable public field named balance and we
can use that to mutate the value of each.


PUBLIC FUNCTIONS IN CLASSES

The real power of classes is that they yield objects with state and a public API
that operates on that state. The state could be any mutable variable, array or
any other value that sits inside the object. The public API consists of one or
more functions that operate on that state.

Here's an example:

class CryptoAccount(amount : Nat, multiplier : Nat) {
    public var balance = amount * multiplier;

    public func pay(amount : Nat) {
        balance += amount;
    };
};

let account = CryptoAccount(10, 5);

account.pay(50);


Our CryptoAccount class takes the same two arguments as before, but now has only
two members. One is the public mutable balance variable. The second is a public
function. Because there are two public fields, the expected type of the object
returned from this class is

{
    pay : () -> Nat;
    balance : Nat;
}


After instantiating the account variable with our class, we can access the
public function by calling it as a method of our object. When we write
account.pay(50) we call that function, which in turn mutates the internal state
of the object.

In this example the public function happens to operate on a public variable. It
could also have mutated a private mutable variable of any type.