{"record":{"id":"27dbba58f59339b5","repo":"go-delve/delve","slug":"invalid-argument-s-type-s-to-imag","errorCode":null,"errorMessage":"invalid argument %s (type %s) to imag","messagePattern":"invalid argument (.+?) \\(type (.+?)\\) to imag","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":2029,"sourceCode":"\tr := realev.newVariable(\"\", 0, typ, nil)\n\tr.Value = constant.BinaryOp(realev.Value, token.ADD, constant.MakeImag(imagev.Value))\n\treturn r, nil\n}\n\nfunc imagBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {\n\tif len(args) != 1 {\n\t\treturn nil, fmt.Errorf(\"wrong number of arguments to imag: %d\", len(args))\n\t}\n\n\targ := args[0]\n\targ.loadValue(loadSingleValue)\n\n\tif arg.Unreadable != nil {\n\t\treturn nil, arg.Unreadable\n\t}\n\n\tif arg.Kind != reflect.Complex64 && arg.Kind != reflect.Complex128 {\n\t\treturn nil, fmt.Errorf(\"invalid argument %s (type %s) to imag\", astutil.ExprToString(nodeargs[0]), arg.TypeString())\n\t}\n\n\treturn newConstant(constant.Imag(arg.Value), arg.bi, arg.mem), nil\n}\n\nfunc realBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {\n\tif len(args) != 1 {\n\t\treturn nil, fmt.Errorf(\"wrong number of arguments to real: %d\", len(args))\n\t}\n\n\targ := args[0]\n\targ.loadValue(loadSingleValue)\n\n\tif arg.Unreadable != nil {\n\t\treturn nil, arg.Unreadable\n\t}\n\n\tif arg.Value == nil || ((arg.Value.Kind() != constant.Int) && (arg.Value.Kind() != constant.Float) && (arg.Value.Kind() != constant.Complex)) {","sourceCodeStart":2011,"sourceCodeEnd":2047,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L2011-L2047","documentation":"Delve's `imag()` builtin only accepts complex64 or complex128 values. This error is returned when the (single) argument's reflect.Kind is not a complex kind, including the argument's source text and type in the message.","triggerScenarios":"Evaluating `imag(x)` where x's Kind is Int, Float, String, etc. — i.e. not Complex64/Complex128 — after its value loaded successfully (eval.go:2029, imagBuiltin).","commonSituations":"Calling imag on a plain float (imagining floats have imaginary parts), or on an interface-typed value that Delve could not resolve to a complex concrete type.","solutions":["Pass a complex-typed value: verify with `ptype <expr>` that its kind is complex64/complex128.","For a float, the imaginary part is 0 — just use 0 instead of imag(f).","If the value is an interface, evaluate the concrete field or assert its type first.","Use `real(c)` if you actually wanted the real part."],"exampleFix":"// before\nimag(f)          // f is float64\n// after\nimag(c)          // c is complex128\nreal(f)          // or real for float input","handlingStrategy":"type-guard","validationCode":"// before evaluating imag(x)\nif v.Kind != reflect.Complex64 && v.Kind != reflect.Complex128 {\n    return fmt.Errorf(\"imag requires complex64/complex128, got %s\", v.TypeString())\n}","typeGuard":"func isComplexKind(v *proc.Variable) bool {\n    return v.Kind == reflect.Complex64 || v.Kind == reflect.Complex128\n}","tryCatchPattern":"val, err := evalImag(expr)\nif err != nil && strings.Contains(err.Error(), \"invalid argument\") {\n    // fall back to 0 for floats, or report the type mismatch\n}","preventionTips":["Check ptype output: imag only works on complex64/complex128.","For plain floats, the imaginary part is just 0.","Resolve interface values to their concrete complex type first."],"tags":["debugger","go","expression-evaluation","complex-numbers","type-mismatch"],"backgroundTag":"invalid-builtin-argument","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}