{"id":"ca3088be31b124fe","repo":"gohugoio/hugo","slug":"can-t-apply-the-operator-to-the-values","errorCode":null,"errorMessage":"can't apply the operator to the values","messagePattern":"can't apply the operator to the values","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/math/math.go","lineNumber":51,"sourceCode":"\t\tswitch bv.Kind() {\n\t\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\t\tisInt = true\n\t\t\tbi = bv.Int()\n\t\tcase reflect.Float32, reflect.Float64:\n\t\t\tisFloat = true\n\t\t\taf = float64(ai) // may overflow\n\t\t\tbf = bv.Float()\n\t\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:\n\t\t\tbu = bv.Uint()\n\t\t\tif ai >= 0 {\n\t\t\t\tisUint = true\n\t\t\t\tau = uint64(ai)\n\t\t\t} else {\n\t\t\t\tisInt = true\n\t\t\t\tbi = int64(bu) // may overflow\n\t\t\t}\n\t\tdefault:\n\t\t\treturn nil, errors.New(\"can't apply the operator to the values\")\n\t\t}\n\tcase reflect.Float32, reflect.Float64:\n\t\tisFloat = true\n\t\taf = av.Float()\n\t\tswitch bv.Kind() {\n\t\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\t\tbf = float64(bv.Int()) // may overflow\n\t\tcase reflect.Float32, reflect.Float64:\n\t\t\tbf = bv.Float()\n\t\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:\n\t\t\tbf = float64(bv.Uint()) // may overflow\n\t\tdefault:\n\t\t\treturn nil, errors.New(\"can't apply the operator to the values\")\n\t\t}\n\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:\n\t\tau = av.Uint()\n\t\tswitch bv.Kind() {\n\t\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/common/math/math.go#L33-L69","documentation":"common/math.DoArithmetic implements Hugo's arithmetic template functions (add, sub, mul, div) via reflection. It only supports int/uint/float operands (plus string+string concatenation for '+'). If either operand's reflect.Kind is anything else — bool, nil, map, slice, time, or an unparsed string — it returns this error.","triggerScenarios":"Calling `add`, `sub`, `mul`, or `div` in a template where an operand is not numeric: e.g. `{{ add .Params.count 1 }}` when the front matter value is the string \"3\", `{{ sub \"a\" \"b\" }}`, adding a nil value from a missing param, or `add` with a bool/map/slice.","commonSituations":"Front matter or config values quoted as strings (weight: \"10\"), missing .Params keys yielding nil, values coming from data/JSON where numbers were encoded as strings, or trying to use `add` for string concat with a non-string second operand (string concat only works when both are strings and op is '+').","solutions":["Convert operands explicitly with `int`, `float`, or `string` before the math call, e.g. `{{ add (int .Params.count) 1 }}`.","Guard against nil/missing params with `default`, e.g. `{{ add (.Params.count | default 0) 1 }}`.","Fix the source data so numbers are unquoted in front matter/config/data files."],"exampleFix":"{{/* before */}}\n{{ add .Params.count 1 }}  {{/* count: \"3\" in front matter */}}\n\n{{/* after */}}\n{{ add (int (.Params.count | default 0)) 1 }}","handlingStrategy":"type-guard","validationCode":"// Both operands must be numeric (or both strings for +)\nif !isNumeric(a) || !isNumeric(b) { /* cast first */ }","typeGuard":"func isNumeric(v any) bool {\n\tswitch reflect.ValueOf(v).Kind() {\n\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,\n\t\treflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,\n\t\treflect.Float32, reflect.Float64:\n\t\treturn true\n\t}\n\treturn false\n}","tryCatchPattern":"v, err := math.DoArithmetic(a, b, op)\nif err != nil {\n\t// report which operand had the wrong type; do not substitute a default value\n}","preventionTips":["Cast template values with `int`/`float` before arithmetic (front-matter values are often strings).","Only use string operands with '+', never with -, *, /.","Validate user-supplied params at the boundary before passing them to math helpers."],"tags":["hugo","templates","math","type-mismatch"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}