Understanding 4 Different Typesystem In Solidity

Zinzuvadiya Meet
Published in
4 min readJun 5, 2023

--

The preferred programming language for creating smart contracts on the Ethereum blockchain is Solidity. Solidity uses a strong typesystem because it is a statically-typed language, which enhances the security and effectiveness of your smart contracts by enabling exact data management.

We will examine the four primary sorts of variables in the Solidity typesystem in this blog post: Value Type, Memory Type, User-Defined Type, and Type Aliases. For writing and putting into action smart contracts that communicate with the Ethereum network, it is imperative to have a thorough understanding of these types.

Value Type

You can declare variables in Solidity using a variety of value types. The storage and manipulation of various forms of data in your smart contracts depends on these value types. Solidity’s primary value types are as follows:

  1. Boolean (bool): This type represents a binary value, either true or false. It is stored as an 8-bit unsigned number.
  2. Integer (int and uint): Solidity provides different integer types that vary in size and signedness. The int type represents signed integers, while the uint type represents unsigned integers. You can specify the size of integers using the number of bits, ranging from 8 bits (int8/uint8) to 256 bits (int256/uint256).
  3. Address (address and address payable): The address type is used to store Ethereum addresses. It is a 160-bit value and is typically used for representing user accounts or contracts on the Ethereum network. The address payable type is a variant of address that allows the transfer of Ether.
  4. Bytes (bytes): The bytes type is used to store sequences of bytes. It can have a variable length, ranging from 1 to 32 bytes (bytes1 to bytes32). Bytes are stored from left to right, with each byte represented by its hexadecimal value.

Here are a few important things to note about Solidity’s value types:

  • Unsigned numbers are stored as big-endian, with the high bits to the left. For example, 0x00000001 represents the decimal value 1.
  • Signed numbers are stored as big-endian in two’s complement form, with the high bits to the left. For example, 0xffffffff represents the decimal value -1.
  • Addresses are stored as unsigned numbers with a length of 160 bits.
  • Booleans are stored as 8-bit unsigned numbers.
  • Bytes are stored as a sequence of bytes from left to right, with each byte represented by its hexadecimal value. For example, 0x80000000 represents the decimal value 1.

Memory Type

When handling complex data structures within functions or between function calls, Solidity uses the Memory Type, commonly known as the Link Type or Reference Type. It gives you more freedom for handling data while a contract is being executed by allowing you to interact with dynamically-sized arrays, strings, and structs. Solidity’s primary memory types are:

  1. Bytes: The Bytes type allows you to store densely packed raw bytes. It is useful when you want to handle and manipulate byte-level data directly.
  2. String: The String type represents closely packed characters encoded in UTF-8 format. It allows you to work with strings of varying lengths and perform string operations efficiently.
  3. Array of Fixed Length: You can declare arrays with a fixed length using the Memory Type. These arrays store elements of a specific type and have a predetermined number of elements that cannot be changed during execution.
  4. Array of Dynamic Length: The Memory Type also supports arrays with a dynamic length. These arrays can grow or shrink during execution, allowing for flexibility when dealing with data collections of varying sizes.

Let’s take a look at an example to understand how Memory Type variables are stored and accessed:

User-defined Type

Structs are a strong feature in Solidity that gives programmers the ability to construct unique data structures. A struct is a user-defined type that allows the collection of related data into a single unit by allowing the inclusion of many properties or fields. Within Solidity smart contracts, this encourages code organisation, readability, and maintainability.

Structs give you the ability to design unique data structures that are specific to the needs of your smart contract. They enable you to group similar data into useful units, improving the readability and maintainability of your code. In Solidity contracts, structures are especially helpful for describing composite data structures or real-world things.

Here’s an example of defining a struct in Solidity:

Type Aliases

Type aliases serve as aliases for existing types in Solidity, providing a way to create alternative names for types or to create more descriptive names for complex types. They contribute to code clarity and help in conveying the purpose and intention of variables or functions. Read More

Here’s an example of aliases type in Solidity:

👋🏻 In this blog post, we have explored the Solidity type system, which plays a vital role in ensuring the security and effectiveness of smart contracts on the Ethereum blockchain. We discussed the four main types of variables: value types, memory types, user-defined types (such as structs), and type aliases. Next, we will dive into the various storage locations for Solidity variables. These locations determine where variables are stored and accessed on the blockchain.

--

--

Zinzuvadiya Meet
Coinmonks

Computer scientist with a passion for blockchain. Interest in cryptography and basic Solidity skills. familiarity with tools like web3.js, truffle & Ethereum.