Solidity is the Weirdest: 3 Things You Desperately Need To Know

Caleb Stultz
devslopes
Published in
5 min readFeb 7, 2018
Maybe not that weird, but still…

Solidity is a weird language. It has some quirks and “features” that make it super frustrating for experienced developers but not everyone feels this way. I personally enjoy using Solidity and working around it’s limitations but we’re here to learn the three things –types, specifically–that make Solidity so unique.

If you’ve done any personal research on blockchain development, the word Solidity should be familiar. Solidity is a programming language developed by the team that brought you Ethereum. It is statically typed, supports inheritance, libraries, and complex user-defined types. It was based on C++, JavaScript, and Python so it should at least look familiar. It can be used to create smart contracts for voting, crowdfunding, blind auctions, multi-signature crypto wallets, and more.

The smartest of contracts…

And that’s sort of the point of Solidity existing — smart contracts. Essentially, a smart contract is a collection of code and data that live at a specific address on the Ethereum blockchain. You can declare variables and store data within them as seen below.

pragma solidity ^0.4.0;contract Storage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public constant returns (uint) {
return storedData;
}
}

In this example, storedData is a state variable of type UInt (256 bits by default). It’s like a single slot in a database that can be queried and altered by calling the functions that manage the database. Something unique about Solidity is that you don’t need to use the keyword this to access state variables. The functions set() and get() are used to retrieve and modify the value of storedData as you may expect.

Obviously, the contract Storage doesn’t do much, but it serves as a primitive example of how Smart Contracts look and operate. Let’s move on to exploring some of the unique types in Solidity.

Address

In Solidity, there is a type called address and it is a 160-bit value used for storing the wallet address of a contract. It must be set as public and when the code is compiled, a function is auto-generated allowing you to access the current value of that variable. This is especially relevant if you want to have multiple contracts working together.

contract Auction {
address public beneficiary;
}

Variables of type address have access to several members that add to its’ functionality: balance ,transfer, send, call, callcode, and delegatecall. Developers are able to query the balance of an account and transfer Ether to another address using these built-in functions, however send is a low-level version of transfer and will simply return false if the transaction fails wheras transfer will stop the current contract with an exception. The function call returns a boolean indicating whether or not an invoked function returns true or false.

Mapping

Next up, I’m going to touch on the Solidity type called mapping. It is essentially like a dictionary or hash table in most other programming languages. It accepts key-value pairs which is perfect for storing pairs of addresses and values.

contract Account {
mapping(address => uint) balances;
}

There are limitations with mapping variables, however. You can’t yet get a list of all the keys in a mapping or a list of all values, so you’ll need to know what is in there beforehand or use it in a context when you can provide the information needed like in the function below.

function balances(address account) public view returns (uint) {
return balances[account];
}

Event

After that is the event type. An event can be fired from a function when a certain condition is met. User interfaces can listen for those events being fired and the listener can receive arguments from, to, and amount which makes it easy to track and display transaction information as seen in the example below.

contract Store {
event PurchaseComplete(address from, address to, uint amount);
function purchaseItem(address seller, uint amount) public payable {
// Code to purchase goes here...
PurchaseComplete(msg.sender, seller, msg.value);
}
}

Other than these 3 special types, many other common value and reference types exist like Booleans, Integers, Arrays, Strings, Enums, Functions Types, and Structs and they work similarly to how they work in other programming languages.

The documentation on Solidity is pretty robust overall and you can check it out at solidity.readthedocs.io. They also have a Solidity simulator called Remix which allows you to play around with Solidity and even check out example code. You can find that at remix.ethereum.org.

If you want to learn more about Blockchain development, head over to www.devslopes.com and enroll in our Blockchain Developer Slope where you’ll learn everything you need to know to develop smart contracts, distributed apps, and more. ❤️

--

--