apache/beam · error
Incompatible func type: got func
Error message
Incompatible func type: got func %v with %v inputs and %v outputs, want 2 inputs and 2 outputs
What it means
ToFunc2x2 adapts a generic reflectx Func into a Func2x2 (2 inputs, 2 outputs). It panics when the wrapped function's reflect type does not have exactly 2 inputs and 2 outputs, surfacing the mismatch when the wrapper is created.
Solutions
- Make the function take 2 inputs and return exactly 2 values.
- Use the ToFunc/MakeFuncNxM variant matching the real arity.
- Validate NumIn()/NumOut() before adapting.
Example fix
// before
reflectx.MakeFunc2x2(func(a, b int) int { return a + b }) // panics: 1 output
// after
reflectx.MakeFunc2x2(func(a, b int) (int, int) { return a + b, a - b }) Defensive patterns
Strategy: validation
Validate before calling
t := c.Type()
if t.NumIn() != 2 || t.NumOut() != 2 {
return fmt.Errorf("func %v has %d ins/%d outs; ToFunc2x2 needs 2 ins/2 outs", t, t.NumIn(), t.NumOut())
} Type guard
func isFunc2x2(c reflectx.Func) bool {
return c.Type().NumIn() == 2 && c.Type().NumOut() == 2
} Try / catch
func safeToFunc2x2(c reflectx.Func) (f reflectx.Func2x2) {
defer func() {
if r := recover(); r != nil {
log.Fatalf("ToFunc2x2 arity mismatch: %v", r)
}
}()
return reflectx.ToFunc2x2(c)
} Prevention
- Match inputs and outputs counts explicitly when writing 2x2 functions.
- Update variant selection whenever a return value is added or removed.
- Include wrapper construction in CI tests.
When it happens
Trigger: Calling reflectx.ToFunc2x2 with a Func whose Type().NumIn() != 2 or Type().NumOut() != 2.
Common situations: Binary functions updated to return an extra value; copy-pasted adapter calls between arity variants; merge functions whose signature changed during refactoring.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Incompatible func type: got func
- Incompatible func type: got func
- Incompatible func type: got func
- Incompatible func type: got func
- Incompatible func type: got func
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/905880f6ef73aaea.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/go/pkg/beam/core/util/reflectx/calls.go:557
}
func (c *shimFunc2x2) Type() reflect.Type {
return c.inner.Type()
}
func (c *shimFunc2x2) Call(args []any) []any {
return c.inner.Call(args)
}
func (c *shimFunc2x2) Call2x2(arg0, arg1 any) (any, any) {
ret := c.inner.Call([]any{arg0, arg1})
_ = ret
return ret[0], ret[1]
}
func ToFunc2x2(c Func) Func2x2 {
if c.Type().NumIn() != 2 || c.Type().NumOut() != 2 {
panic(fmt.Sprintf("Incompatible func type: got func %v with %v inputs and %v outputs, want 2 inputs and 2 outputs", c.Type(), c.Type().NumIn(), c.Type().NumOut()))
}
if sc, ok := c.(Func2x2); ok {
return sc
}
return &shimFunc2x2{inner: c}
}
func MakeFunc2x2(fn any) Func2x2 {
return ToFunc2x2(MakeFunc(fn))
}
type Func2x3 interface {
Func
Call2x3(any, any) (any, any, any)
}
type shimFunc2x3 struct {
inner FuncView on GitHub (pinned to 12126d8942)