-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: reflect: add Value.CanCall method #39545
Comments
When does this come up in practice? |
@ianlancetaylor I was working in something like this: ...
method := reflect.ValueOf(str).MethodByName(methodName)
if !method.IsValid() {
return nil, errors.New("structMethod Invalid Fn Arg")
}
res := method.Call(inputs)
... Calling a struct method by his name in order to do some generic stuff for a particular problem. The way to avoid the panic is to code all the checks again and I dont think its the fn responsibility. |
The implementation of |
@dsnet That is what I am trying to avoid, because everyone who wants to implement a secure call is going to have to add these validations, which do not have to do with the purpose of what is being programmed or import an external package that does it. |
Is there benefit from being able to ask CanCall without actually doing the Call? Also, Call panics with a much more useful message than the bool that CanCall as proposed would return. It could be CheckCall returning an error, of course, but it's still duplicate work if you're just going to Call next anyway. What's the context here where CanCall without Call is an important operation? |
@sgrodriguez, did you see my comment from last week? We are having trouble understanding a context where CanCall is not immediately followed by Call. If Call will happen anyway, it is safer and cheaper to have one function and recover the panic. |
@rsc Sorry for not answering, the pandemic is hitting hard here (100 days of mandatory quarantine and counting) and I was off for a few days. Let me explain a little bit the context of this issue. I'm currently developing a library that needs something like this. It has a function that receives an struct and some variables. This function needs to execute a method exported by the struct received with the variables as parameters. This is implemented using reflection. When calling the method it could panic, so I thought two ways of dealing with it.
I think that the first option is not completly correct because it couples the entire solution with reflect implementation. So whenever the implementation of Call change, my library would break. |
Recovering from a panic is well defined. If you are always going to use |
Just to echo what Ian said, recovering from defined panics is a fine thing to do. Based on the discussion above, this seems like a likely decline. |
I agree. if the recover is well define makes no sense to change reflect call. |
No change in consensus, so declined. |
(Forgot to close this back in July, sorry.) |
Introduction
If you want to call a method with a slice of Value's as input of the method the inner function of call runs diferents checks if one of them fails ie reflect: Call with too few input arguments it panic.
Why not have a method like:
So we avoid panic as we have with CanInteface, CanAddr.
This is the actual checks that call make:
IMHO will refactor some checks in order to reuse in CanCall.
The text was updated successfully, but these errors were encountered: