[Solidity] How to write pragma (version specification)?

This section explains how to write pragma (version specification) to be written at the beginning of the code.

What is the meaning of writing pragma?

The compiler version can be specified by writing pragma.

What is a compiler?

A compiler is "a program that converts our code into a form that a machine can understand".

The code is easy for us to understand because it is written in English, but in order for the computer to execute it, it must be converted into machine language.

Why is it necessary to specify the compiler version?

Compilers are constantly being improved, and new versions appear regularly.

If you wrote code a long time ago, it may not be handled well by the latest compiler.

It is similar to the case that an old application does not work well on the latest OS.

Therefore, by specifying the compiler version, such as "This source code must be processed by this version of the compiler", we can prevent version mismatches between compilers.

How to write pragma?

There are two ways to write pragma.

Caret (batch specification)

pragma solidity ^0.x.x;

When a version is prefixed with a caret (^), it means "from that version to the full minor version (the number in the middle)".

pragma solidity ^0.8.17;

In the above case, the version (0.8.17) to the full minor version (0.9.0) means that the corresponding compiler version is "0.8.17 to less than 0.9.0".

Inequality (range specification)

pragma solidity >=0.x.x <0.x.x;

When an inequality sign is added to the beginning of a version, it means "up to the extent of the version that corresponds to the inequality sign".

pragma solidity >=0.7.0 <0.9.0;

In the above case, >= 0.7.0 (greater than 0.7.0) and <0.9.0 (less than 0.9.0), which means that the corresponding compiler version is "0.7.0 to less than 0.9.0".

You can either use caret to specify the version all at once, or use inequality to specify a detailed range. You can use either of these depending on the situation at the time. For now, let's just keep in mind that "pragma" at the beginning of the code means compiler version specification.

タイトルとURLをコピーしました