www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - $ should be a compile-time constant, if used inside a const array

reply Don Clugston <dac nospam.com.au> writes:
----
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
next sibling parent reply Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
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
parent Don Clugston <dac nospam.com.au> writes:
Oskar Linde wrote:
 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"[$];
 ------------

 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 :)
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. :-)

 dmd -c -o- bug.d
 (has anyone used -o- before?)
Hmm... -c -o- seems like it might as well be a NOP...?
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.
Dec 07 2005
prev sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----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