I want to use some variables in multiple functions in my library contract. Is there any way to do that? I got following error code for my library code.
library myArithmetic {
bytes1[10] mData;
function doSomeMath(bytes1[10] _input) public returns (bytes1[10]) {
for(uint8 i=0 ; i<10 ; i++){
mData[i] = _input[i];
}
//do some my math..here..
}
}
I have error, like TypeError: Library cannot have non-constant state variables
I do not want to make each of my functions with several parameter input and return, and to another functions..so on.
Is it possible to do that??
I know in the normal contract, the mData will be storage variables. But library does not allow to have state variables. So I cannot define such this way?
Best Answer
An approach to solve this is to define a struct inside your library and pass that struct as the first parameter. The functions in your library will be able to modify the struct.
In your contract you create an instance of that struct and pass it to the library functions. You can use the "using" syntax sugar to have a better looking code.