Thread overview
Why is Phobos' isIntegral different from the built-in trait of the very same name?
Oct 18, 2021
Walter Bright
Oct 17, 2021
Walter Bright
October 17, 2021
According to https://github.com/dlang/phobos/blob/master/std/traits.d#L6191:

=====
Detect whether `T` is a built-in integral type. Types `bool`, `char`, `wchar`, and `dchar` are not considered integral.
=====

Not considered by whom? Certainly they are considered by the language per __traits(isIntegral). Also all conversion rules scream "they're integral!"

We even use the built-in trait to create a _different_ one!

Why do we have a slightly different with no added usabilty in the standard library?
October 17, 2021

On 10/17/21 10:36 AM, Andrei Alexandrescu wrote:

>

According to https://github.com/dlang/phobos/blob/master/std/traits.d#L6191:

=====
Detect whether T is a built-in integral type. Types bool, char, wchar, and dchar are not considered integral.

Not considered by whom? Certainly they are considered by the language per __traits(isIntegral). Also all conversion rules scream "they're integral!"

We even use the built-in trait to create a different one!

Why do we have a slightly different with no added usabilty in the standard library?

isIntegral dates back to D1, where __traits didn't exist. The current implementation is a "shorter" version of what was already there, without having to consider code breakage. This is the oldest thing I could find on github:

https://github.com/dlang/phobos/commit/eec6be69edec9601f9f856afcd25a797e845c181#diff-b7918b17cede734a2dd4ecbf2a981d597a5978f52e02f9a3c6f53c9abd797f05R322-R327

Note that this is an import from the old subversion repository, so attribution and PRs/etc. doesn't exist. You can keep looking on dsource.org if you want to find the original commit, but I'm almost positive it was Walter.

I think the original didn't include bool/[w|d]char either by carelessness or a belief that these things really shouldn't be integers (a great irony considering the contemporary pushback from Walter against splitting bool out from the integral family).

Things like this should have never been done, but now we have to live with it. Honestly, if we want to fix it, I would rather have the language enforce what std.traits says. So many problems happen by allowing e.g. integer promotion from char to dchar.

-Steve

October 17, 2021
On 10/17/21 11:07 AM, Steven Schveighoffer wrote:
> On 10/17/21 10:36 AM, Andrei Alexandrescu wrote:
>> According to https://github.com/dlang/phobos/blob/master/std/traits.d#L6191:
>>
>> =====
>> Detect whether `T` is a built-in integral type. Types `bool`, `char`, `wchar`, and `dchar` are not considered integral.
>> =====
>>
>> Not considered by whom? Certainly they are considered by the language per __traits(isIntegral). Also all conversion rules scream "they're integral!"
>>
>> We even use the built-in trait to create a _different_ one!
>>
>> Why do we have a slightly different with no added usabilty in the standard library?
> 
> `isIntegral` dates back to D1, where `__traits` didn't exist. The current implementation is a "shorter" version of what was already there, without having to consider code breakage. This is the oldest thing I could find on github:
> 
> https://github.com/dlang/phobos/commit/eec6be69edec9601f9f856afcd25a797e845c181#diff-b7918b17cede734a2dd4ecbf2a981d597a5978f52e02f9a3c6f53c9abd797f05R322-R327 
> 
> 
> Note that this is an import from the old subversion repository, so attribution and PRs/etc. doesn't exist. You can keep looking on dsource.org if you want to find the original commit, but I'm almost positive it was Walter.
> 
> I think the original didn't include bool/[w|d]char either by carelessness or a belief that these things really shouldn't be integers (a great irony considering the contemporary pushback from Walter against splitting bool out from the integral family).
> 
> Things like this should have never been done, but now we have to live with it. Honestly, if we want to fix it, I would rather have the language enforce what std.traits says. So many problems happen by allowing e.g. integer promotion from `char` to `dchar`.

Interesting, thanks. I saw there's plenty of other isXxx that differ from their built-in counterparts, perhaps with similar histories. I may have written some of that code.

As a pattern, I note just how complex the type system is and how many special cases there are:

* "alias this" shrapnel is all over the place. We should simply state that phobos does not support "alias this" at all. Do conversions on the call side.

* __vector just sticks its nose in random places.

* There's a lot of effort to legitimize enum as a seamless part of the type system - sadly it's not seamless at all.


October 17, 2021
On 10/17/2021 8:07 AM, Steven Schveighoffer wrote:
> Note that this is an import from the old subversion repository, so attribution and PRs/etc. doesn't exist. You can keep looking on dsource.org if you want to find the original commit, but I'm almost positive it was Walter.
> 
> I think the original didn't include bool/[w|d]char either by carelessness or a belief that these things really shouldn't be integers (a great irony considering the contemporary pushback from Walter against splitting bool out from the integral family).

I doubt I would have done one that was so different from the core language's view of an integral type, though of course I can't prove it.

Keep in mind that D1's library largely consisted of work done by people other than me, and a lot was done without a lot of review by me.

You're right that it's far too late to fix this. Hence the purpose behind a Phobos v2.
October 17, 2021
On 10/17/2021 12:19 PM, Andrei Alexandrescu wrote:
> As a pattern, I note just how complex the type system is and how many special cases there are:

and:

> Do conversions on the call side.

More generally, "is a" and "is implicitly convertible to a" must be treated distinctly as separate operations.
October 17, 2021
On 10/17/21 8:13 PM, Walter Bright wrote:
> More generally, "is a" and "is implicitly convertible to a" must be treated distinctly as separate operations.

Word!