Why do 5 of the 6 comparison operators allow a single argument?

{{ if eq 42 }} foo {{ end }} --> error calling eq: missing arguments for comparison 
{{ if ne 42 }} foo {{ end }} --> foo
{{ if gt 42 }} foo {{ end }} --> foo
{{ if ge 42 }} foo {{ end }} --> foo
{{ if lt 42 }} foo {{ end }} --> foo
{{ if le 42 }} foo {{ end }} --> foo

This came up while looking at https://discourse.gohugo.io/t/36941.

I had never tried this before because comparison operators are for… comparing.

It seems like all of the comparison operators should require two arguments.

3 Likes

Prior to 0c251be (PR #6775)…

{{ if eq 42 }} foo {{ end }} --> error calling eq: missing arguments for comparison
{{ if ne 42 }} foo {{ end }} --> wrong number of args for ne: want 2 got 1
{{ if gt 42 }} foo {{ end }} --> wrong number of args for gt: want 2 got 1
{{ if ge 42 }} foo {{ end }} --> wrong number of args for ge: want 2 got 1
{{ if lt 42 }} foo {{ end }} --> wrong number of args for lt: want 2 got 1
{{ if le 42 }} foo {{ end }} --> wrong number of args for le: want 2 got 1

I suggest that you file an issue on GitHub.

To answer the initial question why 5 behave the same and eq does not: eq uses a function that tests the content, all others use simple comparison. I assume that those comparisons understand nil for the right parameter of these equations. n.Eq expects two arguments.

https://github.com/gohugoio/hugo/issues/9462

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.