April 26, 2013
On 04/26/2013 09:11 PM, Walter Bright wrote:
> On 4/26/2013 5:01 AM, Robert Schadek wrote:
>> Anyway, I think no implicit casts would be wonderful, sure everybody
>> would hate it at first but than...
>
> I've used a language with no implicit casts. It didn't get better, and I
> have an enduring dislike of it.
>

What language?

(In my experience it actually gets better.)
April 26, 2013
On Friday, 26 April 2013 at 19:37:48 UTC, Jonathan M Davis wrote:
> The main place where casting would be annoying - if conditions and loop
> conditions - already insert an explicit cast underneat the hood.

IMO it still makes no sense to have the implicit casting done in conditional statements because it is not obvious why a given conditional should treat a zero differently than all other values. However at this stage retaining that old convention is perhaps unavoidable.

At the very least, D should be minimizing the potential for programmers falling into the traps caused by non-intuitive implicit casting. In the case of bool, there's no obvious relationship with the int data type, so let's get rid of that arbitrary association, make casting explicit, and move on.

--rt
April 26, 2013
On 4/26/2013 1:16 PM, Timon Gehr wrote:
> On 04/26/2013 09:11 PM, Walter Bright wrote:
>> On 4/26/2013 5:01 AM, Robert Schadek wrote:
>>> Anyway, I think no implicit casts would be wonderful, sure everybody
>>> would hate it at first but than...
>>
>> I've used a language with no implicit casts. It didn't get better, and I
>> have an enduring dislike of it.
>>
>
> What language?
>
> (In my experience it actually gets better.)

An early Pascal.
April 26, 2013
On 4/26/13, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> An even better example:

import std.stdio;

void foo(bool x) { writeln("1"); }
void foo(long x) { writeln("2"); }

void main()
{
    foo(1);  // "1"
    foo(false ? 2 : 1);  // "2"
}

Kill it with fire.
April 26, 2013
Whatever the choices are of whether bool is a 1-bit integer or a a logical true/false value, this should not happen:
enum e = 1;
void main()
{
   foo(e);  // bool
}

static e = 1;
void main()
{
   foo(e);  // long
}

The reason being that according to the language spec, the constant "1" should be an int. Whether or not it implicitly converts to a bool, this means that "enum e" should have a type of int as well. The actual value shouldn't be taken into account when determining which overload to call, only the type should matter, and an unknown int value should implicitly convert to a long not a bool. Automatic conversion of "1" to bool should only be able to happen at the point where the literal is used if at all:

enum bool e = 1;

Ideally such an implicit conversion would only apply if it was the only possible valid implicit conversion, so this:
foo(1)
Should at least warn of the ambiguity.

Compile-time conversions based on value rather than type are by definition going to break the type system. Therefore the only way to get "1" to normally be an "int" but automatically convert to a "bool" without being inconsistent is to effectively introduce a new type for the literal "1" which has the desired conversions (the same way that zero has a special type in C/C++ which can convert to both an integer or a pointer).

The important thing is that this special type for "1" should never transfer to other things: the type of the literal should be fixed to a normal type at the point where it is used to prevent surprising the user (this is what C/C++ does).

At the moment "enum e = 1;" transfers the special properties of "1" to "e" and I think that's more than a little surprising.
April 26, 2013
On Friday, 26 April 2013 at 06:01:27 UTC, Walter Bright wrote:
> The real issue is do you want to have the implicit conversions:
>
> 0 => false
> 1 => true
>
> or would you require a cast?

The idea of a "true number" and a "false number" doesn't make sense, so yes.
April 26, 2013
On 4/26/2013 12:37 PM, Jonathan M Davis wrote:
> There's nothing whatsoever about bool that
> makes sense as an integral type.

This is where our perspectives sharply diverge. A bool is a 1 bit integer type. Take a look at this, for example:

http://d.puremagic.com/issues/show_bug.cgi?id=9963

Mathematically, they behave like 1 bit integers and are useful that way.
April 26, 2013
On Friday, 26 April 2013 at 21:14:54 UTC, Walter Bright wrote:
> A bool is a 1 bit integer type.

.sizeof returns bytes, not bits, and says that bool is of size 1.
April 26, 2013
On Friday, 26 April 2013 at 21:14:54 UTC, Walter Bright wrote:
> On 4/26/2013 12:37 PM, Jonathan M Davis wrote:
>> There's nothing whatsoever about bool that
>> makes sense as an integral type.
>
> This is where our perspectives sharply diverge. A bool is a 1 bit integer type. Take a look at this, for example:

Uh, uh, that's not a bool. It is a bit (binary digit, just to be sure).

That is: a figure of the base 2. The very definition of bit.

I still have to see some computer course introducing a byte/octet as a group of 8... booleans.

No: boolean is a logical entity and is true or false, while the bit is a figure (a number) and is 0 or 1.

They may overlay for programming purposes to some extent (a bijection, after all), but let's not take the one for another.

Why the computer theory invented the notion of bit? The term of "boolean" was available for centuries.
April 26, 2013
On Friday, 26 April 2013 at 21:01:17 UTC, Brian Schott wrote:
> On Friday, 26 April 2013 at 06:01:27 UTC, Walter Bright wrote:
>> The real issue is do you want to have the implicit conversions:
>>
>> 0 => false
>> 1 => true
>>
>> or would you require a cast?
>
> The idea of a "true number" and a "false number" doesn't make sense, so yes.

I find the current implementation perfectly intuitive and I wouldn´t want it any other way... it models the underlying hardware, just the way it should be.

Sometimes due to bad coding standards I´m forced to write...
if((.....long expression with not immediately apparent operator precedence)!=0)
... absolutely appalling, kills readability with extra () etc. doesn´t matter how many years, I was forced to do it, I still cringe every time I see a line like that and itch to rewrite it more readable.

I also dont know any book(including Knuth), nor online article, which doesn´t clearly define it as 0,1... am very confused by the reactions in this thread, is my background so different from everyone elses?