Skip to main content

Solidity Syntax - Version, Import, Types, Control Structures

Solidity is a contract-oriented programming language used for writing smart contracts on the Ethereum blockchain. In this tutorial, we will discuss the syntax of Solidity.

Version Pragma

Version pragma specifies the version of the compiler that should be used to compile the contract. It is recommended to use the latest version available.

Example:

pragma solidity ^0.5.2;

The above code will compile with a compiler version >= 0.5.2 and < 0.6.0.

Import Files

Import statements allow us to use code from other Solidity files. There are three types of import statements:

  1. Import all symbols from a file:
import "filename"
  1. Import all symbols from a file and assign a symbol to the file:
import * as symbolName from "filename";

or

import "filename" as symbolName;
  1. Import only selected symbols from a file:
import {symbol1 as alias, symbol2} from "filename";

Types

Solidity supports several data types, which include:

Boolean

The bool type in Solidity can have the values true or false.

Operators:

  • Logical: ! (logical negation), && (AND), || (OR)
  • Comparisons: == (equality), != (inequality)

Example:

bool x = true;
bool y = false;

Integer

Solidity supports both unsigned and signed integers.

Unsigned: uint8, uint16, uint32, uint64, uint128, uint256.

Signed: int8, int16, int32, int64, int128, int256.

Operators:

  • Comparisons: <=, <, ==, !=, >= and >
  • Bit operators: &, |, ^ (bitwise exclusive or) and ~ (bitwise negation)
  • Arithmetic operators: +, **, unary **, unary +, ``, /, %, * (exponentiation), << (left shift) and >> (right shift)

Example:

uint256 a = 10;
int256 b = -20;

Address

The address type in Solidity holds an Ethereum address (20-byte value).

The address payable type is similar to address but includes additional methods transfer and send.

Operators:

  • Comparisons: <=, <, ==, !=, >= and >

Methods:

  • balance: address.balance returns the balance of the address in Wei.
  • transfer: address.transfer(amount) sends the given amount of Wei to the address and throws on failure.
  • send: address.send(amount) returns (bool) sends the given amount of Wei to the address and returns false on failure.
  • call: address.call(...) issues a low-level CALL and returns false on failure.
  • delegatecall: address.delegatecall(...) issues a low-level DELEGATECALL and returns false on failure.

Example:

address payable x = address(0x1234567890123456789012345678901234567890);

Array

Solidity supports both dynamic and fixed-sized arrays.

Example:

uint[] dynamicSizeArray;
uint[7] fixedSizeArray;

Fixed Byte Arrays

Fixed byte arrays are declared using the syntax bytesI, where I is an integer between 1 and 32, inclusive. They are used to store binary data of a fixed length. The byte array can be accessed using its index, which starts from 0.

bytes1 myByte;
bytes32 myBytes;

myByte[0] = 0x12;
myBytes[31] = 0xff;

Dynamic Byte Arrays

Dynamic byte arrays are declared using the bytes keyword. They are used to store binary data of a variable length. The byte array can be accessed using its index, which starts from 0. It is similar to the byte[] type.

bytes myBytes;
myBytes.push(0x12);
myBytes.push(0xff);

Enum

An enum is a custom data type that allows a developer to define a set of named values. It is declared using the enum keyword followed by the name of the enum and the values.

enum ActionChoices {
GoLeft,
GoRight,
GoStraight,
SitStill
}

ActionChoices choice = ActionChoices.GoStraight;

Struct

A struct is a user-defined data type that groups together variables of different data types. It is declared using the struct keyword, followed by the name of the struct and the variables.

struct Funder {
address addr;
uint amount;
}

Funder funders;
funders.addr = 0x123;
funders.amount = 1000;

Mapping

A mapping is a data structure that maps a key to a value. It is declared using the mapping keyword, followed by the data type of the key and the data type of the value.

mapping(address => uint) balances;

balances[0x123] = 1000;

Control Structures

Solidity supports most of the control structures from JavaScript, such as if, else, while, do, for, break, continue, return, and the ternary operator ?:. However, the switch and goto statements are not supported.

if (x > 10) {
// do something
} else {
// do something else
}

while (i < 10) {
// do something
i++;
}

for (uint i = 0; i < 10; i++) {
// do something
}

return 42;