Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 07, 2005 $ should be a compile-time constant, if used inside a const array | ||||
---|---|---|---|---|
| ||||
---- const char [] elephant = "abcdef"[2..$]; ---- newbug.d(1): non-constant expression "abcdef"[2..cast(int)(__dollar)] Likewise for: ---------- const char giraffe = "abc"[$]; ------------ Curiosity #1: Did you know? __dollar works as a synonym for $ or length, inside a subscript expression. Curiosity #2: compiles without error if you use: dmd -c -o- bug.d (has anyone used -o- before?) |
December 07, 2005 Re: $ should be a compile-time constant, if used inside a const array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | 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"[$]; > ------------ > Curiosity #1: Did you know? __dollar works as a synonym for $ or 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 :) > Curiosity #2: compiles without error if you use: > dmd -c -o- bug.d > (has anyone used -o- before?) Hmm... -c -o- seems like it might as well be a NOP...? /Oskar |
December 07, 2005 Re: $ should be a compile-time constant, if used inside a const array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oskar Linde | 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"[$]; >> ------------ >> Curiosity #1: Did you know? __dollar works as a synonym for $ or 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. :-) >> Curiosity #2: compiles without error if you use: >> 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. |
January 05, 2006 Re: $ should be a compile-time constant, if used inside a const array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston Attachments: | 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"[$]; > ------------ > Curiosity #1: Did you know? __dollar works as a synonym for $ or length, > inside a subscript expression. > Curiosity #2: compiles without error if you use: > 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 |
Copyright © 1999-2021 by the D Language Foundation