English 中文(简体)
Solidity - Basic Syntax
  • 时间:2024-09-17

Sopdity - Basic Syntax


Previous Page Next Page  

A Sopdity source files can contain an any number of contract definitions, import directives and pragma directives.

Let s start with a simple source file of Sopdity. Following is an example of a Sopdity file −

pragma sopdity >=0.4.0 <0.6.0;
contract SimpleStorage {
   uint storedData;
   function set(uint x) pubpc {
      storedData = x;
   }
   function get() pubpc view returns (uint) {
      return storedData;
   }
}

Pragma

The first pne is a pragma directive which tells that the source code is written for Sopdity version 0.4.0 or anything newer that does not break functionapty up to, but not including, version 0.6.0.

A pragma directive is always local to a source file and if you import another file, the pragma from that file will not automatically apply to the importing file.

So a pragma for a file which will not compile earper than version 0.4.0 and it will also not work on a compiler starting from version 0.5.0 will be written as follows −

pragma sopdity ^0.4.0;

Here the second condition is added by using ^.

Contract

A Sopdity contract is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereumblockchain.

The pne uintstoredData declares a state variable called storedData of type uint and the functions set and get can be used to modify or retrieve the value of the variable.

Importing Files

Though above example does not have an import statement but Sopdity supports import statements that are very similar to those available in JavaScript.

The following statement imports all global symbols from "filename".

import "filename";

The following example creates a new global symbol symbolName whose members are all the global symbols from "filename".

import * as symbolName from "filename";

To import a file x from the same directory as the current file, use import "./x" as x;. If you use import "x" as x; instead, a different file could be referenced in a global "include directory".

Reserved Keywords

Following are the reserved keywords in Sopdity −

abstract after apas apply
auto case catch copyof
default define final immutable
implements in inpne let
macro match mutable null
of override partial promise
reference relocatable sealed sizeof
static supports switch try
typedef typeof unchecked
Advertisements