Skip to content

Instantly share code, notes, and snippets.

@devtooligan
Last active January 21, 2024 14:25
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devtooligan/9031f859469138307d0976455b7ddffa to your computer and use it in GitHub Desktop.
Save devtooligan/9031f859469138307d0976455b7ddffa to your computer and use it in GitHub Desktop.
console.log from pure functions h/t @z0age
// @author original POC by @z0age
library pureConsole {
/*********************
* string + uint256
********************/
function log(string memory errorMessage, uint256 value) internal pure {
_castToPureStringUint(_logStringUint)(errorMessage, value);
}
function _castToPureStringUint(function(string memory, uint256) internal view fnIn)
internal
pure
returns (function(string memory, uint256) pure fnOut)
{
assembly {
fnOut := fnIn
}
}
function _logStringUint(string memory errorMessage, uint256 value) internal view {
console.log(errorMessage, value);
}
/*********************
* uint256 only
********************/
function log(uint256 value) internal pure {
_castToPureUint(_logUint)(value);
}
function _castToPureUint(function(uint256) internal view fnIn)
internal
pure
returns (function(uint256) pure fnOut)
{
assembly {
fnOut := fnIn
}
}
function _logUint(uint256 value) internal view {
console.log(value);
}
/*********************
* string only
********************/
function log(string memory value) internal pure {
_castToPureString(_logString)(value);
}
function _castToPureString(function(string memory) internal view fnIn)
internal
pure
returns (function(string memory) pure fnOut)
{
assembly {
fnOut := fnIn
}
}
function _logString(string memory value) internal view {
console.log(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment