Coinmonks

Coinmonks is a non-profit Crypto Educational Publication. Other Project — https://coincodecap.com/…

Follow publication

Solidity’s ‘using’ keyword

Gustavo (Gus) Guimaraes
Coinmonks
Published in
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:

pragma solidity ^0.4.15;library SomeLibrary  { function add(uint self, uint b) returns (uint) {
return self+b;
}
}
contract SomeContract {

using SomeLibrary for uint;

function add3(uint number) returns (uint) {
return number.add(3);
}
}

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:

pragma solidity ^0.4.15;library SomeLibrary  {function add(uint self, uint b) returns (uint) {
return self+b;
}
}
contract SomeContract {

function add3(uint number) returns (uint) {
return SomeLibrary.add(number, 3);
}
}

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:

pragma solidity ^0.4.15;library SomeOtherLibrary  {   function add(uint self, uint b) returns (uint) {
return self+b;
}
function checkCondition(bool value) returns (bool) {
return value;
}
}contract SomeContract {
using SomeOtherLibrary for *;
function add3(uint number) returns (uint) {
return number.add(3);
}

function checkForTruthy(bool checker) returns (bool) {
return checker.checkCondition();
}
}

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Coinmonks
Coinmonks

Published in Coinmonks

Coinmonks is a non-profit Crypto Educational Publication. Other Project — https://coincodecap.com/ & Email — gaurav@coincodecap.com

Gustavo (Gus) Guimaraes
Gustavo (Gus) Guimaraes

Written by Gustavo (Gus) Guimaraes

A curious mind, joie de vivre practitioner

Responses (4)

Write a response

Thank you for this

--

thanks man, you made it simple!

--

Such a simple and useful blog post!

--