• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Behavior of case() for boolean values

Stats

  • Locked Locked
  • Replies 3
  • Subscribers 143
  • Views 6975
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Behavior of case() for boolean values

Paul Staerke
Paul Staerke over 2 years ago

Hello,

I guess this question was already asked several times, but I could not find a fitting post.

To handle booleans with case(), the values must be specified differently than for normal constants due to the special meaning of 't' and 'nil'. The documentation gives an example, where these cases are given as single-element lists. However, this does not work for me. Here an example (the case code is directly from the manual)

foreach(mapcar myBool '(nil t (nil) (t) 1)
  case( myBool
    (nil 'never) ; this will never match, it is a list of no keys
    (( nil ) nil) ; matches nil
    (( t ) t) ; matches t
    (t (error "Expected t or nil"))
  )
);foreach

=> (never t t t t)

However, when I modify the code this way, it works

foreach(mapcar myBool '(nil t (nil) (t) 1)
  case( myBool
    ('nil nil) ; matches nil
    ('t t) ; matches t
    (t "other")
  )
);foreach

=> (nil t "other" "other" "other")

Is there maybe something wrong with my setup or environment settings?

Best regards
Paul

  • Cancel
  • Andrew Beckett
    Andrew Beckett over 2 years ago

    Hi Paul,

    The example is not really correct. In the initial example (based on the document), the nil clause does match (so it's misleading to say that it will never match!). The (nil) clause won't match because the nil has already been caught by the previous clause. The (t) clause serves as a default in the same way as t so it always matches if it gets that far (I'm not convinced this is correct, but we probably can't change it safely without breaking code that depends on this behaviour).

    The case function can match t if the clause is a list containing t and something else. So for example if the clause was (5 t) it would match either 5 or t. The reason that using 't works is that this is the same as (quote t) which means that either quote or t matches - as can be seen here:

    foreach(mapcar myBool '(nil t (nil) (t) 1 quote)
      case( myBool
        (nil "nil") ; matches nil
        ('t "t") ; matches t or quote
        (t "other") ; matches anything else
      )
    );foreach                                         

    which returns:

    ("nil" "t" "other" "other" "other" "t")

    Hope that helps a bit!

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Paul Staerke
    Paul Staerke over 2 years ago in reply to Andrew Beckett

    Hello Andrew,

    thank you for your quick answer. This behavior is a bit confusing. Maybe this can be fixed in the manual in the future? As a solution, I am using now (t t) as case condition, which also works and only matches t.

    Best regards
    Paul

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 2 years ago in reply to Paul Staerke

    Hi Paul,

    Yes, I'll request an update to the documentation tomorrow when I'm in the office (I'd planned to do this anyway).

    Regards,

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information