Came across this script teaching people how to use library, but I don't understand why the function testFind should be external in the following script. The definition of external function is : external functions are part of the contract but can only be used externally and called outside the contract by the other contracts. But in this case, we are calling the testFind function from inside of Freedom contract. So why is the function external?
my 2nd question is why is the function inside the library internal? does it mean that the contract Freedom "inherit" the library?
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
library IndexOf{
function findIndex(uint[] storage arr, uint x) internal view returns(uint){
for (uint i=0; i<arr.length; i++){
if (arr[i] == x){
return i;
}
}
revert("index not found");
}
}
contract Freedom{
uint[] arr = [3,2,1];
using IndexOf for uint[];
function testFind(uint x) external view returns(uint y ){
return arr.findIndex(x);
}
}
Thank you for your patience!