"Using" Keyword and Passing Object as Parameter? #2634
Unanswered
michaelinwords
asked this question in
Q&A
Replies: 2 comments 2 replies
-
Good read (to resolve the confusion w/ the |
Beta Was this translation helpful? Give feedback.
2 replies
-
@michaelinwords Really struggling to understand! Could you please elaborate with examples of what you were expecting and what it is? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, all! I'm at 4:28:11 in the 32-hour course (Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript), where Patrick introduces the line
using PriceConverter for uint256;
as a way of attaching the PriceConverter library to uint256. This allows us to call the library's functions on any instance of uint256.My question, however, is what's happening with how this "using" keyword is passing the object? My initial thought was that in practice it would look like:
In reality, the object/uint256 is passed as a parameter non-explicitly, a feature that seems to be from Solidity's using keyword, not some other general pattern inherited from JavaScript (as I initially imagined). No problems with this part.
But, then, I thought I would do as I've seen in other languages: in the library's functions, I would just have the first parameter be called "self" to make it unambiguous. This appears to work fine when a function is actually called in the FundMe.sol contract, but the PriceConverter library throws an error if a uint256 doesn't actually call the function somewhere in FundMe.sol. When no FundMe.sol uint256 calls the function, PriceConverter does not allow to defining a parameter for that passed object in the function (for example:
function get_version(uint256 self)
ORfunction get_price(uint256 self)
-- I've seen in many other languages that even if the function doesn't actually use the passed object, it should be fine (not cause errors) to pass it in.This set of behaviors seems really strange and difficult to predict to me, so I figure I'm missing a couple concepts, maybe from Solidity's particular way of doing things or perhaps larger, general programming structures. 🔍 What specific concepts do you all see at play here? Why can functions have that subtly-passed object parameter defined only when the function is actually called on an object/uint256? 🔍
This is my first posting here, so please be kind in pointing out mistakes/gaps in my understanding!
Beta Was this translation helpful? Give feedback.
All reactions