Thread overview
Doubt about char.min/max == typeid(char)
Oct 07, 2022
matheus
Oct 07, 2022
torhu
Oct 07, 2022
matheus
Oct 07, 2022
bauss
Oct 07, 2022
ag0aep6g
October 07, 2022
Hi,

Could anyone please tell me why the properties of min/max of a char returns a "char type" and not a value as an int?

I just got this while playing around:

void main(){
    import std.stdio;
    writeln(char.max); // "nothing"
    writeln(typeid(char.max)); // "char"
    writeln(cast(int)char.max); // 255
}

Thanks in advance,

Matheus.
October 07, 2022

On Friday, 7 October 2022 at 00:13:59 UTC, matheus wrote:

>

Hi,

Could anyone please tell me why the properties of min/max of a char returns a "char type" and not a value as an int?

Well, why whould the highest and lowest values of a type be of a different type..?

import std;

void func(char x) { writeln("It's a char"); }
void func(int x)  { writeln("It's an int"); }
void func(double x)  { writeln("It's a double"); }

void main(){
    func(char.min);
}
October 07, 2022
On Friday, 7 October 2022 at 01:02:57 UTC, torhu wrote:
> On Friday, 7 October 2022 at 00:13:59 UTC, matheus wrote:
>> Hi,
>>
>> Could anyone please tell me why the properties of min/max of a char returns a "char type" and not a value as an int?
>
> Well, why whould the highest and lowest values of a type be of a different type..?

Hmm well I was thinking the min/max as a range/limits, in this case 0 to 255 or it could be -128 to 127 if signed, or in a case of a bool: 0 to 1, but now I see it returns the min/max value of the type itself, like the latter false/true.

I had my reasoning based on my experience with C.

Thanks,

Matheus.

October 07, 2022
On Friday, 7 October 2022 at 04:40:26 UTC, matheus wrote:
>
> Hmm well I was thinking the min/max as a range/limits, in this case 0 to 255 or it could be -128 to 127 if signed

char casts implicitly to int, so if you really need it as an integer type just type your variable as such.

```d
int a = char.max; // valid.
```

If you don't need to assign it then you can cast it.

```
void a(int x) { ... }

a(cast(int)char.max);
```

Even though in the example above the cast isn't necessary, if you want to be sure a(int) is called then you must cast it, since an overload of char will obviously be picked.

>, or in a
> case of a bool: 0 to 1, but now I see it returns the min/max value of the type itself, like the latter false/true.
>

The same applies for a bool.

> I had my reasoning based on my experience with C.
>

D isn't C, so you can't really compare it 1:1, even though a lot of C rules apply to D, then all of them doesn't.

This is one of the places where it improves on top of it, because generally you don't want your min/max to be different types, but you can implicitly get the integer behavior if you desire so.
October 07, 2022
On 07.10.22 07:06, bauss wrote:
> If you don't need to assign it then you can cast it.
> 
> ```
> void a(int x) { ... }
> 
> a(cast(int)char.max);
> ```
> 
> Even though in the example above the cast isn't necessary, if you want to be sure a(int) is called then you must cast it, since an overload of char will obviously be picked.

You can also do that without a cast:

    a(int(char.max));

Does the same thing of course, but it's less dangerous than casting.