Skip to content

Confusing boolean logic #4399

Closed
Closed

Description

@uncenter

To reproduce:

The Less preview doesn't want me to use the correct share link so here's the Less for it:

@foo: 1;
@bar: a;
@foo-is-one: boolean(@foo = 1);
@bar-is-a: boolean(@bar = a);
@is-qar: boolean(@foo-is-one = @bar-is-a);
/* @is-baz: boolean((@foo = 1) = (@bar = a)); */

:root {
  --qar: @is-qar;
/*   --baz: @is-baz; */
}

Current behavior:

Commenting in the @is-baz variable on line 6 (and line 10 to use it in an expression) causes the error variable @is-baz is undefined. As you can see, the expression @is-baz contains is the same is the (working) @is-qar expression, but inlines the @foo-is-one and @bar-is-a checks.

Expected behavior:

There shouldn't be any undefined variable, because the variable is defined — I should be allowed to inline boolean expressions / logical comparisons like so.

Environment information:

  • less version: latest
  • nodejs version: n/a
  • operating system: n/a

Activity

  1. dosubot commented on Mar 3, 2026

    @dosubot

    This is a parser design limitation rather than a bug. When you write boolean((@foo = 1) = (@bar = a)), the parser creates Condition objects for each comparison (@foo = 1 and @bar = a), but these Condition objects cannot be used as operands in another comparison.

    Your working example succeeds because boolean() returns actual boolean keyword values (true/false) when evaluated. When you store those in @foo-is-one and @bar-is-a, they become regular values that can be compared.

    The parser only allows multiple conditions to be combined through logical and/or operators, not through nested comparisons. So you'd need to stick with the intermediate variable approach you've already found.

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about less.js Share Dosu with your team

  2. matthew-dean commented on Mar 10, 2026

    @matthew-dean
    Member

    It's really unclear from your example what the correct input is and what your expected output is. Can you simplify?

  3. uncenter commented on Mar 10, 2026

    @uncenter
    Author

    Yeah of course.

    The type of expression I'm trying to make work is comparing the result of two boolean comparisons. I want to get the output of a = b, where a itself is a comparison expression and b itself is also a comparison expression.

    The following Less code works:

    @foo: 1;
    @bar: a;
    @baz: boolean(boolean(@foo = 1) = boolean(@bar = a)); // true
    
    :root {
      --baz: @baz; // (true)
    }

    It shows that --baz is true. However, if I take away the boolean function wrapper around the expressions @foo = 1 and @bar = a:

    @foo: 1;
    @bar: a;
    @baz: boolean((@foo = 1) = (@bar = a)); // should still be true?
    
    :root {
      --baz: @baz; // compile error
    }

    I get this error: variable @baz is undefined.

    For what it is worth, the JavaScript equivalent of this expression can be seen in the following:

    Image

    Where baz is showing that the two comparisons have the same result.


    In redescribing the problem, I think there are a couple takeaways:

    1. Should Less require the boolean function within an existing boolean expression (aka inside another boolean() function call)? This is shown by the first Less example working and the second not.
    2. If this is the intended behavior - just use the boolean function when doing these kind of weird comparisons - perhaps this is more of a vague error issue where the error message is incorrect: the error should probably tell me that my expression is invalid, rather then the variable being undefined.
  4. matthew-dean commented on Jun 16, 2026

    @matthew-dean
    Member

    @uncenter The error message is bad but Less supports a media-query-like or container-query-like syntax, so the correct way to write it is:

    @foo: 1;
    @bar: a;
    @baz: boolean((@foo = 1) and (@bar = a));
    
    :root {
      --baz: @baz; // true
    }
  5. uncenter commented on Jun 17, 2026

    @uncenter
    Author

    That comparison is slightly different - in that, @baz will show false if the two comparisons are both false. My earlier logic was checking if the comparisons returned the same boolean, not if they were both true.

    @foo: 1;
    @bar: a;
    @baz: boolean((@foo = 100) and (@bar = abcdef));
    
    :root {
      --baz: @baz; // false but true expected
    }

    If you replace the and with an = sign, Less errors again with a nonsensical message.

    @foo: 1;
    @bar: a;
    @baz: boolean(boolean(@foo = 100) = boolean(@bar = abcdef)); // true
    
    :root {
      --baz: @baz; // true
    }

    This is still true even though foo is not 100 and bar is not abcdef, because both are false and the comparison results are the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions