DEV Community

Cover image for How to Layout of a Solidity File?
Mansoor Ahmed
Mansoor Ahmed

Posted on

How to Layout of a Solidity File?

Overall a solidity file ends with “.sol”. This may cover multiple contracts that do not have to have the same name as the filename. It begins with the pragma-line that describes for which compiler version the contract(s) was written.


Pragma solidity ^0.4.24;

Contract My

Version Pragma
Source files may be explained with a supposed version pragma to reject being compiled with future compiler versions. That might present mismatched changes. We try to keep such changes to a total least. Introduce special changes in a way, in which changes in semantics will also need changes in the syntax. But this is of course not always possible. Due to that, this is continuously a good impression to read through the changelog at least for announcements that contain breaking changes. The version pragma is used as follows:

pragma solidity ^0.4.0;
A source file would not accumulate with a compiler previous than version 0.4.0. This will too not work on a compiler starting from version 0.5.0. This second condition is added by using ^. There would be no violation changes until version 0.5.0. This is the actual idea behind this, so we can continuously be sure that our code will compile the way we planned it to. We do not answer the exact version of the compiler. So, bug fix releases are still possible. It is likely to need much more complex rules for the compiler version. The expression trails those used by npm.

Importing other Source Files
Syntax and Semantics

The Solidity does not know the concept of a default export. It supports import statements that are very alike to those obtainable in JavaScript. We can use import statements of the following form:

import “filename”;

This statement imports all global symbols from filename into the current global scope.

import * as symbol name from “filename”;

It is a new global symbol name. Its elements are all the global symbols from the filename.
import {symbol1 as alias, symbol2} from “filename”;
This may create new global symbols alias and symbol2 which reference symbol1 and symbol2 from “filename”, respectively.
Another Syntax is:

import “filename” as symbol name;

This is equivalent to import * as symbol name from “filename”. It is possible to import other contracts from other files. This may be done in many ways from different bases. It is likely to import local files in Remix and files from GitHub. In Truffle it is not possible to import files from GitHub. If we import contracts from other files we can either import the whole file with all containing contracts or just some contracts from the file. Directly from the Solidity documentation: 40T

The filename is continuously preserved as a path with/as a directory separator like a current and parent directory. It is not measured as the current or the parent directory when. Or.. is followed by a character except /. All path names are pickled as absolute paths but they start with the current or the parent directory…Use import “./x” as x to import a file x from the same directory as the current file. If we use import “x” as x; instead, a different file could be referenced. It is determined on the compiler, how to actually resolve the paths. Overall, the directory hierarchy does not essential to severely map onto local filesystem, it may also map to capitals exposed via e.g. ipfs, http or git.

Comments in Solidity
Line and block comments are being supported by Solidity, in addition to natspec-style comments:

// line comment

/// line comment


Block comment
author Thomas Wiesner
Natspec style
Single-line (//) and multi-line (//) comments are possible.

// This is a single-line comment.


This is a multi-line comment.

Moreover, there is an extra type of comment called a natspec comment. There is no such documentation written for natspec comment. They are written with a triple-slash (///) and double asterisk block (/**… */). They should be used straight above function declarations. We may use Doxygen-style tags inside these comments to document functions.
For more details visit:

Discussion (0)