apache/beam · error

Incompatible func type: got func

Error message

Incompatible func type: got func %v with %v inputs and %v outputs, want 0 inputs and 2 outputs

What it means

reflectx.ToFunc0x2 adapts a Func to Func0x2 (0 inputs, 2 outputs) and panics unless the reflect type has exactly 0 inputs and 2 outputs. The panic message reports the actual function type with its input and output counts.

Solutions

  1. Use ToFunc0x3 (or the matching N-output adapter) if the function returns three values.
  2. Remove extra inputs so the signature is exactly func() (A, B).
  3. Compute NumIn/NumOut from the reflect type and select the adapter programmatically before invoking.

Example fix

// before
reflectx.ToFunc0x2(reflectx.MakeFunc(func() (int, int, error) { ... }))
// after
reflectx.ToFunc0x3(reflectx.MakeFunc(func() (int, int, error) { ... }))
Defensive patterns

Strategy: validation

Validate before calling

func isFunc0x2(fn reflectx.Func) bool {
    return fn.Type().NumIn() == 0 && fn.Type().NumOut() == 2
}

Type guard

func asFunc0x2(fn reflectx.Func) (reflectx.Func0x2, bool) {
    if f, ok := fn.(reflectx.Func0x2); ok { return f, true }
    return nil, false
}

Try / catch

func safeToFunc0x2(fn reflectx.Func) (f reflectx.Func0x2, err error) {
    defer func() {
        if r := recover(); r != nil { err = fmt.Errorf("ToFunc0x2: %v", r) }
    }()
    return reflectx.ToFunc0x2(fn), nil
}

Prevention

When it happens

Trigger: Calling ToFunc0x2 or MakeFunc0x2 with a func whose signature is not func() (A, B) — e.g. func() (A, B, C) (three returns) or func(x A) (B, C) (one input).

Common situations: Adapting a supplier that returns value plus two auxiliaries (e.g. (T, metadata, error)); adding a context parameter to an existing 0x2 function without switching adapters.

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

Appendix: source

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

}

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

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

func (c *shimFunc0x2) Call0x2() (any, any) {
	ret := c.inner.Call([]any{})
	_ = ret
	return ret[0], ret[1]
}

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

func MakeFunc0x2(fn any) Func0x2 {
	return ToFunc0x2(MakeFunc(fn))
}

type Func0x3 interface {
	Func
	Call0x3() (any, any, any)
}

type shimFunc0x3 struct {
	inner Func

View on GitHub (pinned to 12126d8942)