Home > Mobile >  Is there a way to check if value satisfy type constraint defined in interface?
Is there a way to check if value satisfy type constraint defined in interface?

Time:08-02

For example I defined the following interface:

type MyTypeConstraint interface {
    ~int | ~string
}

Is there a way to check if given value satisfies this constraint aside from comparing value to each type inside MyTypeConstraint using switch reflect.TypeOf(v) statement? For example, here I don't want user to pass v which type doesn't satisfy constraint into function:

type ErrNode struct {
    Data map[string]interface{}
    // Err  error -- irrelevant
    // next *ErrNode -- irrelevant
}

// `GetData()` is constrained but `Set()` is not.
// Of course I could pass `ErrNode` as parameter into `Set()` 
// like bellow but then I won't be able to chain `Set()` calls.
func GetData[T ErrData](list *ErrNode, k string) (v T, ok bool) {
    v, ok = list.Data[k].(T)
    return v, ok
}

func (e *ErrNode) Set(k string, v interface{}) (self *ErrNode) {
    e.Data[k] = v // v must be of type listed in `MyTypeConstraint`
    return e
}

CodePudding user response:

You cannot (currently with go 1.18) put a type constraint on a method:

//syntax error: method must have no type parameters

func (e *ErrNode) Set[T MyTypeConstraint](k string, v T) (self *ErrNode) {
    e.Data[k] = v
    return e
}

You may put a type constraint on a function:

func Setter[T MyTypeConstraint](e *ErrNode, k string, v T) {
    e.Data[k] = v
}
  • Related