-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Syntax to refer to a specific overload of a function #3556

Comments
Any progress? |
Sorry, no progress. Do you have any suggestion for the syntax? |
@chriseth, sure:
|
Isn't this issue very similar to #526? Maybe whatever solution will be found can be used for both |
Yep, sounds similar! |
The workaround: contract Selector {
function transfer(address, uint) external {}
function transfer(address) external {}
function calculateSelector() public pure returns (bytes4) {
function (address) external t;
return t.selector;
}
} But assigning the correct function to the |
Should we remove the |
Yes, not a bug. |
However, calling Maybe I don't understand how this should be used. |
IIUC it's not really a working workaround. The idea was that
But we should probably try to solve this issue properly. |
@chriseth so what about the proposed synthax? #3556 (comment) |
Existing syntax create ambiguity, which could be a source of potential errors. Providing arguments looks as remedy for such errors, as it was mentioned above:
|
@MikaelLazarev moreover we already have similar syntax for abi.decode: abi.decode(data, (address,uint256,bytes)) |
@chriseth is off now so we won't get an answer from him until February but for me this syntax looks ambiguous. In the code below, would contract C {
enum Enum { A, B, C }
function g(Enum e) public {}
function g(uint i) public {}
function f() public {
uint Enum = 42;
this.g(Enum).selector; // ambiguous
}
} I think we we need different syntax for this. Here are some ideas:
|
@cameel why it should look ambiguous if the param list contains types instead of expressions/values? |
Type names are not necessarily reserved keywords. They can be just normal identifiers. Well, technically the ambiguity can be resolved but either way of doing it prevents you from doing something: in my example if you treat the type name as shadowed by a variable, it prevents you from being able to get a pointer to Also, it's IMO not great for humans either. With contract, struct, enum names, etc. it might not be clear at all that what you're looking at is a function pointer and not a function call. I'd rather have clearer syntax for that. |
Unless you mean that appending |
@cameel Does the same problem with type names and variable names exist with abi.decode method, when you will provide Enum as parameter in types list: Adding |
Just to try to elaborate on the concern about the syntax in #3556 (comment): My preferred solution to the issue at hand at the moment would be to treat it as a plain regular cast - as @cameel noted that doesn't work right now, because functions cannot be casted using the current conversion syntax, but it's on our agenda to rework that anyways, so hopefully this will yield a nice solution killing two birds with one stone... |
@MikaelLazarev You're right. Still, I really don't like how much of a special case that syntax would be. Like @ekpyron said, there's no magic with current
True, it would be a bit different than the other |
Since #11284 would give us an easy way out of this, posting feedback there is likely this speed up getting to a solution here too. We need opinions on the proposed syntax (or new proposals). |
Right now function pointers have argument types defined in compile-time and selector defined in run-time:
Let's add the ability to have
Then
|
While I also like the conversion syntax, it is a bit weird at type level, since you apply a function (the conversion function) to an object that does not have a fixed value. I think we have to go with some special syntax like |
I'm not sure whether I understood @k06a 's proposal correctly, but a function pointer with a fixed selector is essentially an interface with a single defined function. Is this a possible way out here? |
@chriseth yes, but the interface is static at compile-time and a function pointer is dynamic. |
What about this variation:
|
We need a syntax to resolve overloads in a generic way, not a syntax specific to the selector function. |
How about the same way as web3js or ethersjs do?
|
imo, one of the variations of the 2nd approach ( contract C {
function g() public {}
function g(uint256 i) public {}
function f() public {
// assign func to a variable
function(uint256) fn0 = g@(uint256);
function() fn1 = g@();
// fetch selector
bytes4 s0 = this.g@(uint256).selector;
bytes4 s1 = this.g@().selector;
// pass func as an argument
_callFn(fn0);
// etc....
}
function _callFn(function(uint256) fn) internal {
fn(1337);
}
} |

Not sure what could be a good notation and/or a generic way for this. As a workaround a function type can be defined.
Split off #3544.
EDIT by @cameel: Renamed the issue since we often defer to it as the main issue about resolving ambiguous overloads. The original title was
Support retrieving selector of an overloaded function
.The text was updated successfully, but these errors were encountered: