Skip to content
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

reflect.InterfaceValue does not have Implements(*reflect.InterfaceType) bool method #327

Closed
gopherbot opened this issue Nov 24, 2009 · 5 comments

Comments

@gopherbot
Copy link
Contributor

by wmundt42:

There is currently no way, via the reflection interface, to do a
conditional set.  Such a set would have the semantics of the two-result
type assertion in the language itself, but would allow the use of such
semantics on types passed around as variables.

Proposed semantics:

func (v *InterfaceValue) ConditionalSet(x Value) (ok bool)
{
        if x implements v or x is the zero interface value {
                set v to x;
                returns true;
        } else {
                set v to zero interface value;
                return false;
        }
}

This would allow reflection-based checks on whether a value implements an
interface, and also (by construction of zero Value instances) whether any
reflect.Type implements a particular reflect.InterfaceType without having
to iterate the methods and evaluate the logic manually.
@robpike
Copy link
Contributor

robpike commented Nov 24, 2009

Comment 1:

Given a reflect.Value v and a desire to see if the underlying object implements
interface Foo, 
use the value's Interface() method to extract an interface object that can be tested
using a type 
assertion.
if foo, ok := v.Interface().(Foo) {
  use foo
}
That should give you what you need.
-rob

Status changed to WontFix.

@gopherbot
Copy link
Contributor Author

Comment 2 by wmundt42:

Not quite, because it still doesn't say how, given a reflect.InterfaceType object and
a value (either in native or reflect.Value form) how to determine if the value
implements the interface.  You can't put a reflect.InterfaceType into the parens of a
type assertion.

@rsc
Copy link
Contributor

rsc commented Nov 25, 2009

Comment 3:

You don't really need the conditional set,
just the test.

Owner changed to r...@golang.org.

Status changed to LongTerm.

@rsc
Copy link
Contributor

rsc commented Dec 2, 2009

Comment 4:

Labels changed: added packagechange.

@rsc
Copy link
Contributor

rsc commented Apr 20, 2011

Comment 5:

This issue was closed by revision e1ee3b5.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants