bool and int

Chris Angelico rosuav at gmail.com
Tue Jan 24 20:14:50 EST 2023


On Wed, 25 Jan 2023 at 10:32, <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
>
> On 2023-01-25 at 08:58:06 +1100,
> Chris Angelico <rosuav at gmail.com> wrote:
>
> > On Wed, 25 Jan 2023 at 08:22, MRAB <python at mrabarnett.plus.com> wrote:
> > > For backwards compatibility, bool was made a subclass of int.
> >
> > Plus, it's really REALLY handy in quite a lot of situations.
> >
> > > > C# is pickier, which I guess is a good thing.
> > >
> >
> > Nope, not a good thing. Actually a highly frustrating thing on those
> > occasions when I have to write C# code.
>
> The usual complaint is that some people write FORTRAN no matter what
> language they're actually using.  Are you writing Python in C#?  ;-)

Well, let's see. If I were writing C code, I would write:

if (state & PRELAUNCH)

If I were writing Python, it would probably be very different, but it
depends on the API. Could be:

if "PreLaunch" in state:

But the way I have to write it in C# is a messed-up version of C:

if ((state & StartState.PreLaunch) > 0) {

because bool and int are fundamentally different. Using the C style results in:

VelocimeterModule.cs(37,8): error CS0029: Cannot implicitly convert
type `PartModule.StartState' to `bool'

Here's another example. If I were writing C, I would write:

if (TimeWarp_CurrentRateIndex)

Python?

if TimeWarp.CurrentRateIndex:

C#?

if (TimeWarp.CurrentRateIndex > 0)

And, again, if I do it C style, I get:

VelocimeterModule.cs(261,17): error CS0029: Cannot implicitly convert
type `int' to `bool'

I'm pretty sure I've had a case where I wanted to use a boolean in an
arithmetic context, too, but it's less obvious from the final code, so
I can't give an example. So this is synthetic:

autothrust_last_dv *= AT_mode == AT.Idle;

VelocimeterModule.cs(252,4): error CS0019: Operator `*=' cannot be
applied to operands of type `double' and `bool'

So the problem isn't that I'm trying to write Python in C#, but that
I'm trying to write code that would work on pretty much *any other
C-family language*, but doesn't work on C#. I could use those
techniques in plenty of C-derived and C-inspired languages, but nooooo
not in C#, despite looking very much C-inspired. Unfortunately the
truth is that C# is not *actually* C-inspired; it's really Microsoft
Java, so it has all the stupidities of Java:

int x = 3 + (args.length > 1);
test.java:4: error: bad operand types for binary operator '+'

if (args.length) System.out.println("There are args!");
test.java:6: error: incompatible types: int cannot be converted to boolean

But this is hardly a Python-versus-C# thing; it's Java versus most of
the rest of the world, and C# feigns to be part of the C style while
retaining the limitations of Java.

(My apologies if the Java entries look synthetic. It's because they
are, and that's a consequence of me not having ANY reason to write
Java code in, like, ever. In fact, I had to go and install a JDK just
to confirm that Java really did have these limitations.)

ChrisA


More information about the Python-list mailing list