apache/beam · error

Incompatible func type: got func

Error message

Incompatible func type: got func %v with %v inputs and %v outputs, want 1 inputs and 4 outputs

What it means

ToFunc1x4 adapts a generic reflectx Func into a Func1x4 (1 input, 4 outputs). It panics when the wrapped function's type does not have exactly 1 input and 4 outputs. The guard ensures arity errors are caught when the wrapper is built.

Solutions

  1. Make the wrapped function take 1 input and return exactly 4 values.
  2. Switch to the matching arity variant (e.g. ToFunc1x3).
  3. Assert c.Type().NumIn()==1 && c.Type().NumOut()==4 before wrapping.

Example fix

// before
reflectx.MakeFunc1x4(func(x int) (int, int, int) { return x, x, x }) // panics
// after
reflectx.MakeFunc1x4(func(x int) (int, int, int, int) { return x, x, x, x })
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

func isFunc1x4(c reflectx.Func) bool {
    return c.Type().NumIn() == 1 && c.Type().NumOut() == 4
}

Try / catch

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

Prevention

When it happens

Trigger: Calling reflectx.ToFunc1x4 or MakeFunc1x4 with a Func whose Type().NumIn() != 1 or Type().NumOut() != 4.

Common situations: Wrapping a function with a different return count after refactoring; mis-selecting the generated helper among the NxM variants in calls.go.

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/79e998de2d272607. Report an issue: GitHub.

Appendix: source

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

}

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

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

func (c *shimFunc1x4) Call1x4(arg0 any) (any, any, any, any) {
	ret := c.inner.Call([]any{arg0})
	_ = ret
	return ret[0], ret[1], ret[2], ret[3]
}

func ToFunc1x4(c Func) Func1x4 {
	if c.Type().NumIn() != 1 || c.Type().NumOut() != 4 {
		panic(fmt.Sprintf("Incompatible func type: got func %v with %v inputs and %v outputs, want 1 inputs and 4 outputs", c.Type(), c.Type().NumIn(), c.Type().NumOut()))
	}
	if sc, ok := c.(Func1x4); ok {
		return sc
	}
	return &shimFunc1x4{inner: c}
}

func MakeFunc1x4(fn any) Func1x4 {
	return ToFunc1x4(MakeFunc(fn))
}

type Func2x0 interface {
	Func
	Call2x0(any, any)
}

type shimFunc2x0 struct {
	inner Func

View on GitHub (pinned to 12126d8942)