Solidity Syntax - Using For, Error Handling, Global Variables
In this tutorial, we will cover Solidity syntax, specifically on using the using - for keyword, error handling, global variables, mathematical and cryptographic functions, and contract-related keywords.
Using - For
The using A for B; syntax in Solidity can be used to attach library functions to any type. For example, the following code attaches a library function called add from the arithmatic library to the uint type:
library arithmatic {
function add(uint _a, uint _b) returns (uint) {
return _a + _b;
}
}
contract C {
using arithmatic for uint;
uint sum;
function f(uint _a) {
sum = _a.add(3);
}
}
In the above code, the library arithmatic contains a function called add that takes two uint arguments and returns their sum. We then use the using arithmatic for uint syntax to attach the add function to the uint type. This allows us to call the add function on any uint variable, as shown in the f function.