Thread overview
Compiler does not always honor typedef contract
Dec 05, 2005
Roberto Mariottini
Dec 05, 2005
Walter Bright
Dec 07, 2005
Roberto Mariottini
Dec 18, 2005
Thomas Kuehne
December 05, 2005
Hi,
Tried with DMD v0.141 on Windows XP:

typedef int apples;
typedef int pears;

int main(char[][] args)
{
int i = 13;
apples a = 13; // OK (?)
apples a2 = i; // cannot implicitly convert expression (i) of type int to apples
pears p = 13;  // OK (?)
pears p2 = i;  // cannot implicitly convert expression (i) of type int to pears

a = a + a; // OK
a = a + 1; // OK (?)
a = a + i; // OK (?)
a = a + p; // OK (???)
a = 1 + a; // cannot implicitly convert expression (1 + a) of type int to apples
a = i + a; // cannot implicitly convert expression (i + a) of type int to apples
a = p + a; // cannot implicitly convert expression (p + a) of type pears to
apples
a = cast(apples) 1 + a; // OK
a = cast(apples) i + a; // OK
a = cast(apples) p + a; // OK

return 0;
}


December 05, 2005
"Roberto Mariottini" <Roberto_member@pathlink.com> wrote in message news:dn1gtg$145u$1@digitaldaemon.com...
> Hi,
> Tried with DMD v0.141 on Windows XP:
>
> typedef int apples;
> typedef int pears;
>
> int main(char[][] args)
> {
> int i = 13;
> apples a = 13; // OK (?)

Yes. Using typedefs would be useless if this didn't work.

> apples a2 = i; // cannot implicitly convert expression (i) of type int to
apples

Yes. Otherwise there is no point to typedef's. <g>

> pears p = 13;  // OK (?)
> pears p2 = i;  // cannot implicitly convert expression (i) of type int to
pears
>
> a = a + a; // OK
> a = a + 1; // OK (?)

Yes.

> a = a + i; // OK (?)

This should be allowed.

> a = a + p; // OK (???)

This should not be allowed.

> a = 1 + a; // cannot implicitly convert expression (1 + a) of type int to
apples

This should be allowed.

> a = i + a; // cannot implicitly convert expression (i + a) of type int to
apples

This should be allowed.

> a = p + a; // cannot implicitly convert expression (p + a) of type pears
to
> apples

This should not be allowed.

> a = cast(apples) 1 + a; // OK
> a = cast(apples) i + a; // OK
> a = cast(apples) p + a; // OK
>
> return 0;
> }
>
>


December 07, 2005
In article <dn203h$1q0n$1@digitaldaemon.com>, Walter Bright says...
>
>
[...]
>> typedef int apples;
>> typedef int pears;
>>
>> int main(char[][] args)
>> {
>> int i = 13;
>> apples a = 13; // OK (?)
>
>Yes. Using typedefs would be useless if this didn't work.

I understand, even if I disagree. I am for strong typing, for this case I prefer a constructor approach, like:

apples a = apples(13);

And what about function parameter passing?

void f (apples x) { ... }
..
f(13); // is this allowed?

In my opinion this should be an error.

>> apples a2 = i; // cannot implicitly convert expression (i) of type int to
>apples
>
>Yes. Otherwise there is no point to typedef's. <g>
>

Indeed.

[...]
>> a = a + 1; // OK (?)
>
>Yes.

I disagree, see above.

>> a = a + i; // OK (?)
>
>This should be allowed.

How this is different from:

apples a2 = i;

For consistency this should be an error as well.

>> a = a + p; // OK (???)
>
>This should not be allowed.

Obviously.

Ciao


December 18, 2005
Walter Bright schrieb am 2005-12-05:
>
> "Roberto Mariottini" <Roberto_member@pathlink.com> wrote in message news:dn1gtg$145u$1@digitaldaemon.com...
>> Hi,
>> Tried with DMD v0.141 on Windows XP:
>>
>> typedef int apples;
>> typedef int pears;
>>
>> int main(char[][] args)
>> {
>> int i = 13;
>> apples a = 13; // OK (?)

[snip]

>> a = i + a; // cannot implicitly convert expression (i + a) of type int to
> apples
>
> This should be allowed.

[snip]

Where is this documented?



Added to DStress as http://dstress.kuehne.cn/nocompile/t/typedef_09_A.d http://dstress.kuehne.cn/nocompile/t/typedef_09_B.d http://dstress.kuehne.cn/nocompile/t/typedef_09_C.d http://dstress.kuehne.cn/run/t/typedef_09_D.d http://dstress.kuehne.cn/run/t/typedef_09_E.d http://dstress.kuehne.cn/run/t/typedef_09_F.d

Thomas