digitalmars.D.bugs - $ should be a compile-time constant, if used inside a const array
- Don Clugston (13/13) Dec 07 2005 ----
- Oskar Linde (17/31) Dec 07 2005 Useful for obfuscated code...:
- Don Clugston (17/57) Dec 07 2005 Cool! (If you like that sort of thing)
- Thomas Kuehne (15/28) Jan 05 2006 -----BEGIN PGP SIGNED MESSAGE-----
---- const char [] elephant = "abcdef"[2..$]; ---- newbug.d(1): non-constant expression "abcdef"[2..cast(int)(__dollar)] Likewise for: ---------- const char giraffe = "abc"[$]; ------------ inside a subscript expression. dmd -c -o- bug.d (has anyone used -o- before?)
Dec 07 2005
Don Clugston wrote:---- const char [] elephant = "abcdef"[2..$]; ---- newbug.d(1): non-constant expression "abcdef"[2..cast(int)(__dollar)] Likewise for: ---------- const char giraffe = "abc"[$]; ------------ inside a subscript expression.Useful for obfuscated code...: class MyArrayContainer { int length() { return 4; } int opIndex(int i) { return i; } } void main() { MyArrayContainer arr = new MyArrayContainer; int __dollar = 7; writef("%d ",arr[$-1]); arr[$=15]; writef("%d\n",arr[++$]); } prints 6 16 :)dmd -c -o- bug.d (has anyone used -o- before?)Hmm... -c -o- seems like it might as well be a NOP...? /Oskar
Dec 07 2005
Oskar Linde wrote:Don Clugston wrote:Cool! (If you like that sort of thing) Actually, D has some great obfuscation potential. As discussed below, you can print output at compile time, so the top half of a file could be a compile-time program, and the bottom half could be complete garbage that won't even compile. :-)---- const char [] elephant = "abcdef"[2..$]; ---- newbug.d(1): non-constant expression "abcdef"[2..cast(int)(__dollar)] Likewise for: ---------- const char giraffe = "abc"[$]; ------------ length, inside a subscript expression.Useful for obfuscated code...: class MyArrayContainer { int length() { return 4; } int opIndex(int i) { return i; } } void main() { MyArrayContainer arr = new MyArrayContainer; int __dollar = 7; writef("%d ",arr[$-1]); arr[$=15]; writef("%d\n",arr[++$]); } prints 6 16 :)Actually, it isn't. I made a batch file, dd.bat, consisting of just dmd -c -o- %1 Then, using compile-time metafunctions, and using pragma(msg) to print, you can treat it as a complete (interpreted) programming language. eg dd beer.d prints the "99 bottles of beer" song. There's no hint anywhere that you're using a language which is normally compiled :-). I use dd when writing metafunctions, to be certain that everything is happening at compile time.dmd -c -o- bug.d (has anyone used -o- before?)Hmm... -c -o- seems like it might as well be a NOP...?
Dec 07 2005
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Don Clugston schrieb am 2005-12-07:---- const char [] elephant = "abcdef"[2..$]; ---- newbug.d(1): non-constant expression "abcdef"[2..cast(int)(__dollar)] Likewise for: ---------- const char giraffe = "abc"[$]; ------------ inside a subscript expression. dmd -c -o- bug.d (has anyone used -o- before?)Added to DStress as http://dstress.kuehne.cn/run/l/length_08_A.d http://dstress.kuehne.cn/run/l/length_08_B.d http://dstress.kuehne.cn/run/l/length_08_C.d http://dstress.kuehne.cn/run/l/length_08_D.d http://dstress.kuehne.cn/run/l/length_08_E.d Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFDvQ9/3w+/yD4P9tIRAj3vAJ4vPHCWDesCbcetQ7o4nwZI0SpicACfaApa s2beYhEXNpKb8L5WMcVkwd8= =wQ1h -----END PGP SIGNATURE-----
Jan 05 2006