0

Is it possible to determine (using reflect) whether an instance of an arbitrary type can be set to an arbitrary value, i.e. to determine whether Value.Set() would panic due to type incompatibility?

An MCVE is set out below. What I want to know is effectively 'can I write set() without using the defer/recover construct?'

I'd like to avoid defer not only because it seems ugly, but because Value.Set() may panic for other reasons.

Note this is not merely a question of comparing the types for equality, as the o2 example below shows.

package main

import (
    "fmt"
    "reflect"
)

// set a value V to interface i, returning true if this can be done, else false
//
// CAN WE WRITE THIS WITHOUT HAVING TO USE DEFER / RECOVER?
//
func set(v reflect.Value, i interface{}) bool {
    success := true
    defer func() {
        if r := recover(); r != nil {
            success = false
        }
    }()
    v.Set(reflect.ValueOf(i))
    return success
}

// get the type of a typed nil
func getType(typedNil interface{}) reflect.Type {
    return reflect.TypeOf(typedNil).Elem()
}

func main() {
    t1 := getType((*int)(nil))
    o1 := reflect.New(t1)

    t2 := getType((*interface{})(nil))
    o2 := reflect.New(t2)

    var ok bool
    var aInt = 456
    var aString string = "hello"
    var aUint uint = 123

    // Set o1 to various types
    
    ok = set(o1.Elem(), aInt) // Should return true
    fmt.Printf("After o1 set to aInt returned %v: obj is type %T content '%v'\n", ok, o1.Elem().Interface(), o1.Elem().Interface())

    ok = set(o1.Elem(), aString) // Should return false
    fmt.Printf("After o1 set to aString returned %v: obj is type %T content '%v'\n", ok, o1.Elem().Interface(), o1.Elem().Interface())

    ok = set(o1.Elem(), aUint) // Should return false
    fmt.Printf("After o1 set to aUint returned %v: obj is type %T content '%v'\n", ok, o1.Elem().Interface(), o1.Elem().Interface())

    fmt.Println("")

    // Set o2 to various types
    ok = set(o2.Elem(), aInt)  // Should return true
    fmt.Printf("After o2 set to aInt returned %v: obj is type %T content '%v'\n", ok, o2.Elem().Interface(), o2.Elem().Interface())

    ok = set(o2.Elem(), aString) // Should return true
    fmt.Printf("After o2 set to aString returned %v: obj is type %T content '%v'\n", ok, o2.Elem().Interface(), o2.Elem().Interface())
    
    ok = set(o2.Elem(), aUint) // Should return true
    fmt.Printf("After o2 set to aUint returned %v: obj is type %T content '%v'\n", ok, o2.Elem().Interface(), o2.Elem().Interface())
}

1 Answer 1

2

reflect.Type has a Type.AssignableTo() method:

// AssignableTo reports whether a value of the type is assignable to type u.
AssignableTo(u Type) bool

So you may use this to simplify your set() function:

func set(v reflect.Value, i interface{}) bool {
    if !reflect.TypeOf(i).AssignableTo(v.Type()) {
        return false
    }
    v.Set(reflect.ValueOf(i))
    return true
}

Output will be the same (try it on the Go Playground):

After o1 set to aInt returned true: obj is type int content '456'
After o1 set to aString returned false: obj is type int content '456'
After o1 set to aUint returned false: obj is type int content '456'

After o2 set to aInt returned true: obj is type int content '456'
After o2 set to aString returned true: obj is type string content 'hello'
After o2 set to aUint returned true: obj is type uint content '123'

You may also (additionally) call Value.CanSet() to detect certain "unsettable" scenarios:

CanSet reports whether the value of v can be changed. A Value can be changed only if it is addressable and was not obtained by the use of unexported struct fields. If CanSet returns false, calling Set or any type-specific setter (e.g., SetBool, SetInt) will panic.

2
  • Perfect. Thanks. I was sure this should exist but it's mysteriously missing from the index at golang.org/pkg/reflect - though it's there lower down.
    – abligh
    Jan 4, 2021 at 12:52
  • @abligh It's missing in the index because reflect.Type is an interface, not a concrete implementation, and methods of interfaces are not listed in the index, only methods of concrete types.
    – icza
    Jan 4, 2021 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.