Ethereum Smart Contracts with Solidity: Complex Types in Solidity

Ethereum Smart Contracts with Solidity: Complex Types in Solidity


We now take a look at some of the more complex types in
the Solidity language.
Starting with the contract type.
So this is one which we did see when we created a Hello World smart contract,
just a little earlier.
Since smart contracts are essentially represented by a contract address,
it is possible to initialize a contract type from an address.
And we can also create an address type out of a deployed smart contract.
One thing with contract types,
however, is that they do not support any operators.
So just for a reference,
the way to initialize a contract type is by means of the contract keyword,
which is what we have done in this HelloWorld smart contract.
Which takes us now to the string type in the Solidity language.
And these can be initialized with a literal, and
string literals can be defined using either single quotes or double quotes.
So in this respect Solidity is rather similar to JavaScript.
Solidity strings can be implicitly converted to bytes,
which is another type available in the Solidity language.
As touched upon earlier,
Solidity does not include any native string manipulation functions.
And string variables in Solidity are meant to hold values
rather than manipulate them.
A few examples of strings in a solidity smart contract.
We can bring back the HelloWorld example.
So the return type of our function has been defined to be of type string
using the string keyword and note the lower case s here.
We also make use of a string literal
when we return the value HelloWorld which is within single quotes.
We move along now to the mapping types in the Solidity language.
And this is the data structure which is capable of storing key and
value pairs.
These are similar to hash tables in Java for
example or dictionaries in Python.
And while you don’t need the keys and values to be both of the same type.
There are only certain types which are supported for keys.
For example, these can only be of primitive types.
For example, address, string, int and so on.
On the other hand,
the values in your mapping type don’t have any such restrictions.
These can be of type struct, or arrays, or even contracts, or you can
also have other mapping types as the value in your mapping data structure.
And finally we move along to the array types within Solidity.
So arrays over here can have either a fixed length or even a dynamic length.
The elements of the array can be of any type, but
all the elements of a single array must be of the same type.
In this regard,
Solidity arrays are very similar to the ones in other programming languages.
And one thing to keep in mind is that in Solidity, bytes and
string types are in fact special types of arrays.
So with that, you have some idea of the different types which are available
in the Solidity programming language.
One thing to keep in mind when coding any Solidity contract,
however, is that there are no real null types or
undefined types which you have in other programming languages.
A side effect of this is that some variables
will end up having a default value if you don’t set it a value explicitly.
For example, a string variable which has not been explicitly set a value
will actually be equal to a blank string rather than being equal to null.
So if you access a string and it is equal to a blank string,
then you don’t know whether that is a value which has been set explicitly, or
whether it was never assigned a value in the first place.
And you’ll need to code around this.
With that said, we can now move along to creating and
using reference types in the Solidity language.

Add a Comment

Your email address will not be published. Required fields are marked *