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 3 outputs

What it means

ToFunc2x3 adapts a generic reflectx Func into a Func2x3 (2 inputs, 3 outputs). It panics when the wrapped function's type does not have exactly 2 inputs and 3 outputs, so arity errors fail fast at wrapper construction.

Solutions

  1. Make the function take 2 inputs and return exactly 3 values.
  2. Switch to the ToFunc/MakeFuncNxM variant matching the actual signature.
  3. Check c.Type().NumIn()==2 && c.Type().NumOut()==3 before wrapping.

Example fix

// before
reflectx.MakeFunc2x3(func(a, b int) (int, int) { return a, b }) // panics: 2 outputs
// after
reflectx.MakeFunc2x3(func(a, b int) (int, int, int) { return a, b, a + b })
Defensive patterns

Strategy: validation

Validate before calling

t := c.Type()
if t.NumIn() != 2 || t.NumOut() != 3 {
    return fmt.Errorf("func %v has %d ins/%d outs; ToFunc2x3 needs 2 ins/3 outs", t, t.NumIn(), t.NumOut())
}

Type guard

func isFunc2x3(c reflectx.Func) bool {
    return c.Type().NumIn() == 2 && c.Type().NumOut() == 3
}

Try / catch

func safeToFunc2x3(c reflectx.Func) (f reflectx.Func2x3) {
    defer func() {
        if r := recover(); r != nil {
            log.Fatalf("ToFunc2x3 arity mismatch: %v", r)
        }
    }()
    return reflectx.ToFunc2x3(c)
}

Prevention

When it happens

Trigger: Calling reflectx.ToFunc2x3 with a Func whose Type().NumIn() != 2 or Type().NumOut() != 3.

Common situations: Functions with 2 inputs whose return count changed; mis-selecting among the generated NxM helpers in calls.go after adding or removing a return value.

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


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/9843ed50414a75da. Report an issue: GitHub.

Appendix: source

Thrown at sdks/go/pkg/beam/core/util/reflectx/calls.go:598

}

func (c *shimFunc2x3) Type() reflect.Type {
	return c.inner.Type()
}

func (c *shimFunc2x3) Call(args []any) []any {
	return c.inner.Call(args)
}

func (c *shimFunc2x3) Call2x3(arg0, arg1 any) (any, any, any) {
	ret := c.inner.Call([]any{arg0, arg1})
	_ = ret
	return ret[0], ret[1], ret[2]
}

func ToFunc2x3(c Func) Func2x3 {
	if c.Type().NumIn() != 2 || c.Type().NumOut() != 3 {
		panic(fmt.Sprintf("Incompatible func type: got func %v with %v inputs and %v outputs, want 2 inputs and 3 outputs", c.Type(), c.Type().NumIn(), c.Type().NumOut()))
	}
	if sc, ok := c.(Func2x3); ok {
		return sc
	}
	return &shimFunc2x3{inner: c}
}

func MakeFunc2x3(fn any) Func2x3 {
	return ToFunc2x3(MakeFunc(fn))
}

type Func2x4 interface {
	Func
	Call2x4(any, any) (any, any, any, any)
}

type shimFunc2x4 struct {
	inner Func

View on GitHub (pinned to 12126d8942)