www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Compiler does not always honor typedef contract

reply Roberto Mariottini <Roberto_member pathlink.com> writes:
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;
}
Dec 05 2005
parent reply "Walter Bright" <newshound digitalmars.com> writes:
"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

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

 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

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

This should be allowed.
 a = p + a; // cannot implicitly convert expression (p + a) of type pears

 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;
 }

Dec 05 2005
next sibling parent Roberto Mariottini <Roberto_member pathlink.com> writes:
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

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
Dec 07 2005
prev sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

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 -----BEGIN PGP SIGNATURE----- iD8DBQFDpg603w+/yD4P9tIRAuFsAJ91gp/Tk8VHFIzVV187XZ+jzRuCFgCeNEOA gSVYSeOvd1yjImiYjujOx18= =kQg3 -----END PGP SIGNATURE-----
Dec 18 2005