Solidity’s ‘using’ keyword

Gustavo (Gus) Guimaraes
Coinmonks
2 min readAug 22, 2017

--

There comes a time when one wonders the meaning behind certain features in the programming language one enjoys coding with. Lukas Cremer, Gerard and I are blockchain developers based in Berlin, Germany, and we challenged ourselves whether we could explain the use of solidity’s using keyword.

using is used for including a library within a contract in solidity. Check this following example:

The code using SomeLibrary for uint; allows us in this example to use return number.add(3); inside the function add3 . It is basically syntactic sugar for the following:

As we can see, by adding the keyword using we could give any uint within the SomeContract the libraries functions and pass the uintas the first parameter of that function.

Now let’s look at another example:

Here the * allows for any type from the SomeOtherLibrary to be accessed in the contract.

Another thing we realized is that this also works for array and struct types but one needs to declare the storage keyword. But this is a topic for another blog post.

Thanks to Lukas Cremer and Gerard Baecker for helping with this blog post.

Get Best Software Deals Directly In Your Inbox

--

--