Stream: Miscellaneous

Topic: Wy int instead of bool bad?


view this post on Zulip Julin S (Nov 10 2022 at 04:59):

In the C programming language, int values double up as boolean values.

I've been told this is bad.

But why is it bad?

At first, I thought of course it's bad, but couldn't think of any examples
illustrating why it's bad.

Could you help me find some examples?

view this post on Zulip Julin S (Nov 10 2022 at 05:00):

As far type systems in general are concerned.

view this post on Zulip Julin S (Nov 10 2022 at 05:04):

Likewise char and float can both get implicitly converted to int in C'

view this post on Zulip Julin S (Nov 10 2022 at 05:42):

Tried in python with static type checking (mypy). That too has no complaints to make:

def incr(num: int) -> int:
    return num + True

incr(2)
# 3

Last updated: Jan 29 2023 at 18:03 UTC