Last active
January 21, 2024 14:25
-
-
Save devtooligan/9031f859469138307d0976455b7ddffa to your computer and use it in GitHub Desktop.
console.log from pure functions h/t @z0age
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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