I am inheriting the ERC20
contract and overriding its _beforeTokenTransfer
virtual fuction.
In my override, I am only checking who the sender and hence only need the from
parameter and not the virtual method's to
and amount
parameters.
The Solidity compiler is giving me the following warning:
"Unused function parameter. Remove or comment out the variable name to silence this warning."
However, I need to include the unused to
and from
parameters to override the virtual function (or so I think).
Is there a way to avoid this? And does it increase gas costs having unused parameters?
If understanding this correctly, you have a function like:
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual {}
that you want to override but only need from
. In that case, you can simple do:
function _beforeTokenTransfer(
address from,
address,
uint256
) internal overrides { }
to avoid the compiler warning.
2 Likes
Thanks. Do you have any docs/links explaining why this is allowed/so? Or is it just for this exact scenario?
As you're overriding a function, you need to mirror the signature, leading to the need to declare the positions. Thereafter, it's purely stylistic on the part of the compiler - it doesn't change the underlying bytecode.
Regarding the exact scenario, yes - this is the only situation I can think of. The most similar situation would be when you call a function that returns two variables but you only need one of them, but the way to handle that is actually syntactically different:
contract A {
function f() pure internal returns (uint, uint) {
return (1, 2);
}
function foo() external pure returns (uint256) {
// Just use a comma to indicate that the second variable isn't needed
(uint x,) = f();
return x;
}
}