Skip to content

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

@gopherbot

Description

@gopherbot

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions