Skip to main content
30 votes

What are the drawbacks of allowing implicit boolean/integer conversions?

The main drawback of allowing implicit boolean/integer conversions is, it makes it impossible to prevent errors that result from confusion of a boolean and an integer. Next time you confuse the order ...
Eldritch Conundrum's user avatar
24 votes
Accepted

What are the drawbacks of allowing implicit boolean/integer conversions?

There is a particular drawback of allowing implicit conversions (aka coercions*) from bool to int in dynamic languages such as ...
kaya3's user avatar
  • 19.7k
19 votes

Are there any languages where conditions are not first-class expressions?

Yes, there are languages where conditional expressions do not work like C and produce values, which I think is what you mean by "normal language" here. One recent example is Verse, where the ...
Michael Homer's user avatar
  • 13k
18 votes

Are there any languages where conditions are not first-class expressions?

This is true in Bourne Shell, and most of the shells that are derived from it, such as POSIX shell, bash and ksh. There are no ...
Barmar's user avatar
  • 1,901
13 votes
Accepted

How to make logical operators that return operands consistent in a statically typed language?

The logical operators && and || are statically typed in Typescript, where the result is a union type. Many languages don'...
kaya3's user avatar
  • 19.7k
13 votes
Accepted

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

I wouldn't store them as arrays, but you can certainly implement such a syntax efficiently. If you translate x[0] to x & 1 ...
ruarq's user avatar
  • 146
12 votes

Are there any languages where conditions are not first-class expressions?

Many computer architectures work like this at the level of machine code / assembly language. There may be a "condition code" as part of the machine state, such that conditional branches can ...
Nate Eldredge's user avatar
12 votes

Are there any languages where conditions are not first-class expressions?

In Microsoft SQL Server, the Boolean Type is not available as a general-purpose type, and exists only in contexts that require a boolean expression. This is spelled out in the manual for Comparison ...
IMSoP's user avatar
  • 2,428
11 votes

Which number should be considered as true/false?

I think your question is a false dichotomy, at least in a lot of cases; modern high-level languages1 tend not to allow conversion between booleans and integer types, so this should be considered an ...
Philip Kendall's user avatar
9 votes

What are the drawbacks of allowing implicit boolean/integer conversions?

Booleans may not just be 1 or 0 Much of this depends on the underlying representation of the value. My particular experience of this is with C. Before C99, there was no boolean type, only integers ...
Graham's user avatar
  • 281
8 votes

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

It would encourage inefficient code 90% of the time when you're doing individual bit lookups on an int, you're doing something "wrong". E.g., this would encourage those who don't feel ...
Rydwolf Programs's user avatar
8 votes

What are the drawbacks of allowing implicit boolean/integer conversions?

In addition to all the other fine answers, Boolean coercions are not consistent across languages: Zero (0) is considered falsy in Python and JavaScript, but truthy ...
Nayuki's user avatar
  • 181
8 votes

Are there any languages where conditions are not first-class expressions?

This is not uncommon in older versions of BASIC. GW-BASIC, for example, does not have a boolean data type, so there is no way to use a boolean expression except in statements that specifically expect ...
FrederikVds's user avatar
6 votes

Which number should be considered as true/false?

Because the actual register cannot store a bit, and the register has no type, You are probably thinking about a general purpose register. However, CPUs typically also have flags to store certain ...
G. Sliepen's user avatar
6 votes

What are the drawbacks of allowing implicit boolean/integer conversions?

One particularly bad issue with int to bool conversions is that if you have an expression based language, the assignment x=7 will typically return ...
Oscar Smith's user avatar
6 votes

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

Strictly speaking, an "array" is a specific kind of data structure where elements are stored at contiguous, equally-spaced memory addresses. The smallest addressable unit of memory is ...
kaya3's user avatar
  • 19.7k
5 votes

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

It privileges one form of bit access over others Accessing a word as a bit vector can be useful in some circumstances, but even more useful is the ability to pack and unpack general bit fields. Things ...
Pseudonym's user avatar
  • 4,716
5 votes

How to make logical operators that return operands consistent in a statically typed language?

That's a logical operator that returns its operand. I don't know if there is a better term. Python's or return the first true object. if any, or returns the last ...
André L F S Bacci's user avatar
5 votes

What are the drawbacks of allowing implicit boolean/integer conversions?

There are some common errors that result from integer to boolean conversions. One of the most common among C beginners is the strcmp() function. As a comparison ...
Barmar's user avatar
  • 1,901
4 votes

Are there any languages where conditions are not first-class expressions?

Early versions of Basic only allowed relational operators like > and < in IF statements, not in expressions.
Peter Norvig's user avatar
4 votes

Are there any languages where conditions are not first-class expressions?

csh/tcsh From the manpages [1] [2]: Expressions The if, while and exit builtin commands use expressions with a common syntax. [...] Note that the @ builtin command (q.v.) has its own separate syntax. ...
JoL's user avatar
  • 181
3 votes

Are there any languages where conditions are not first-class expressions?

Going back to ancient/archaic languages: traditional COBOL has no concept of boolean and therefore it is impossible to assign an expression to any variable. I believe FORTRAN, and RPG also may have ...
millebi's user avatar
  • 131
3 votes

How to make logical operators that return operands consistent in a statically typed language?

VHDLs boolean operators are overloaded functions. Rather than just a single <boolish> or <boolish> there is a family of such functions, where the ...
user1937198's user avatar
3 votes

How to make logical operators that return operands consistent in a statically typed language?

Such behavior appears semantically equivalent to what functional operations on the Optional type do in several languages. I'll refer to Rust in my example (except ...
abel1502's user avatar
  • 2,517
2 votes

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

As integers (certainly positive integers) are representable in base 2 as binary numbers, and a binary internal representation is also most common, integers arguably are bit (Boolean) arrays. So the ...
LHP's user avatar
  • 156
2 votes

What are the disadvantages of this far-fetched idea: All integers can be treated as boolean arrays?

I do not know of any languages that support this idea Let's fix that :-) Vyxal is a code golf language which allows indexing into an integer as if it was a collection of digits: ...
Adám's user avatar
  • 3,156
2 votes

Are there any languages where conditions are not first-class expressions?

Tcl is another example of such a language. Tcl, the language, has no concept of expressions at all. Tcl only has: statements of which the first word must be a command/function name. Basically ...
slebetman's user avatar
  • 181
1 vote

What are the drawbacks of allowing implicit boolean/integer conversions?

There might not be a sensible, unambiguously correct conversion The popular Numpy third-party library for Python provides a clear case study for this possibility. Because Python is dynamically typed, ...
Karl Knechtel's user avatar
1 vote

How to make logical operators that return operands consistent in a statically typed language?

TL;DR: yes and no We need to take a step back to understand why the question is a bit strange and why programming languages do not behave like this. I'll still provide a Tyr implementation for Integer ...
feldentm's user avatar
  • 1,779

Only top scored, non community-wiki answers of a minimum length are eligible