{"record":{"id":"8eca21307a576b63","repo":"kataras/iris","slug":"bad-value-function-has-zero-inputs-empty-input-f","errorCode":null,"errorMessage":"bad value: function has zero inputs: empty input function must output a single value but got: length=%v, type[0]=%s","messagePattern":"bad value: function has zero inputs: empty input function must output a single value but got: length=(.+?), type\\[0\\]=(.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hero/dependency.go","lineNumber":282,"sourceCode":"\tdest.Handle = handler\n\treturn true\n}\n\nfunc fromFunc(v reflect.Value, dest *Dependency) bool {\n\tif !isFunc(v) {\n\t\treturn false\n\t}\n\n\ttyp := v.Type()\n\tnumIn := typ.NumIn()\n\tnumOut := typ.NumOut()\n\n\tif numIn == 0 {\n\t\t// it's an empty function, that must return a structure.\n\t\tif numOut != 1 {\n\t\t\tfirstOutType := indirectType(typ.Out(0))\n\t\t\tif firstOutType.Kind() != reflect.Struct && firstOutType.Kind() != reflect.Interface {\n\t\t\t\tpanic(fmt.Sprintf(\"bad value: function has zero inputs: empty input function must output a single value but got: length=%v, type[0]=%s\", numOut, firstOutType.String()))\n\t\t\t}\n\t\t}\n\n\t\t// fallback to structure.\n\t\treturn fromStructValue(v.Call(nil)[0], dest)\n\t}\n\n\tif numOut == 0 {\n\t\tpanic(\"bad value: function has zero outputs\")\n\t}\n\n\tif numOut == 2 && !isError(typ.Out(1)) {\n\t\tpanic(\"bad value: second output should be an error\")\n\t}\n\n\tif numOut > 2 {\n\t\t// - at least one output value\n\t\t// - maximum of two output values","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/hero/dependency.go#L264-L300","documentation":"fromFunc validates zero-input functions: an empty function must return exactly one struct or interface value (the container falls back to treating its return as a struct dependency). If the returned type count or kind does not satisfy this, it panics.","triggerScenarios":"Registering func() (a, b) (multiple outputs) or func() someNonStructType (e.g. int, string, slice, map) through NewDependency/resolveDependency.","commonSituations":"Returning a primitive like func() string to provide a config value; forgetting that only struct/interface returns are valid for zero-input funcs; copy-pasting a multi-return factory func() (*Svc, error) into a zero-input position.","solutions":["Make a zero-input function return exactly one struct or interface: func() *MyStruct { ... }.","If you need multiple outputs, use inputs+error signature instead: func(x X) (*Svc, error).","Wrap primitives in a struct: func() Config { return Config{Host: ...} } instead of func() string.","If it's an interface return, ensure the concrete implementation is also registerable/bindable."],"exampleFix":"// before\nfunc port() int { return 8080 } // panic: not struct/interface\n// after\ntype Port int\nfunc port() Port { return 8080 } // named struct-like type? use struct instead:\n// or: func port() Config { return Config{Port: 8080} }","handlingStrategy":"validation","validationCode":"func isValidZeroInputFunc(fn any) bool {\n\tt := reflect.TypeOf(fn)\n\tif t.NumIn() != 0 { return true } // not this error's case\n\tif t.NumOut() != 1 { return false }\n\tk := t.Out(0).Kind()\n\treturn k == reflect.Struct || k == reflect.Interface\n}","typeGuard":null,"tryCatchPattern":"func() { defer func() { if r := recover(); r != nil { log.Fatalf(\"dependency func invalid: %v\", r) } }()\n\tc.Register(fn) }()","preventionTips":["Zero-input factory funcs must return exactly one struct/interface.","Wrap primitives in structs.","Prefer (T, error) signatures with at least one input."],"tags":["dependency-injection","function-signature","panic","reflection"],"backgroundTag":"invalid-dependency-signature","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}