Why is Phobos' isIntegral different from the built-in trait of the very same name?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Oct 17 19:19:31 UTC 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.




More information about the Digitalmars-d mailing list