DEV Community

Cover image for Motoko Types For Beginners
Ano-ly
Ano-ly

Posted on

Motoko Types For Beginners

Motoko is a programming language developed by DFINITY, for building dApps on the Internet Computer.

Just like any other programming language, Motoko has its own variable types which are not very different from what you would encounter in programming languages like JavaScript, C and Python. Motoko takes type conversions very seriously, therefore, as a programmer who's new to the Motoko programming language, it is necessary to understand Motoko types and how to use the functions in their base libraries to convert in between types.

The following are the major types in the Motoko programming language:

1. Nat

This is just a short name for 'natural number'. This type encompasses all integers greater than or equal to zero. It is basically the only type that you wouldn't find in programming languages like Python. There are four bounded Nat types in Motoko. These are Nat8, Nat16, Nat32 and Nat64, representing 8-bit natural numbers, 16-bit natural numbers, 32-bit natural numbers, and 64-bit natural numbers respectively.

2. Int

This type represents all integers, both positive and negative. It might interest you to know that there are also four bounded integer types in Motoko- Int8, Int16, Int32 and Int64. These are 8-bit signed integers, 16-bit signed integers, 32-bit signed integers and 64-bit signed integers respectively. Each of them has its own separate utility functions associated with its base library.

3. Float

This is a type that represents double-precision floating point numbers. The float type can be converted to the Int and Text types using functions in the Float base library. Numbers like 3.55, 2.1, 3.0, etc. are categorized under the Float type in Motoko.

4. Char

This type encompasses characters represented as Unicode code points. 'a', '*', '%', etc. all fall under the Char type. A variable of type Char has to be converted to type Text before it can be concatenated with a string of type Text.

5. Text

This is very similar to the string type in Python and JavaScript. It is a sequence of 'Char's (characters). Float and Int types in Motoko can be converted to Text using utility functions in the Float and Int base libraries respectively.

6. Bool

This type can take the value of either true or false.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay